简体   繁体   English

使用正文的 csv 数据使用 python 发送电子邮件

[英]Sending an email with python using csv data for the body

I am using the csv library to pull data to into an email body.我正在使用 csv 库将数据提取到电子邮件正文中。 I am pulling certain columns from the csv for the body.我正在从身体的 csv 中提取某些列。 I am using a junk gmail account to test.我正在使用垃圾 Gmail 帐户进行测试。 I'm just confused on how to use the for loop.我只是对如何使用 for 循环感到困惑。 If I'm correct, you need a for loop to read the rows then a for loop to email out.如果我是对的,你需要一个 for 循环来读取行然后一个 for 循环来发送电子邮件。 I did read Reading and Writing CSV Files but was uncertain how to implement it with smtplib.我确实阅读了Read and Writing CSV Files,但不确定如何使用 smtplib 实现它。

import csv, smtplib, ssl

message = """Subject: Your Title

{supervisor} {title} {start}"""

from_address = "test@gmail.com"
password = input("Type your password and press enter: ")

context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(from_address, password)
    with open('test.csv','rb') as file:
        reader = csv.reader(file, delimiter=',')
        next(reader)

            #start = []
            #title = []
            #supervisor = []

            for row in reader:
                title = row[3]
                start = row[0]
                supervisor = row[4]

            for supervisor, title, start in reader:
                 server.sendmail(
                    from_address,
                     Email,
                     message.format(supervisor=supervisor, start=start, title=title)
                 )

print('Emails were sent successfully!')  

CSV File: CSV 文件:

StartDate,Employee Name,PC,Title,Supervisor Name,email 
12/9/2019,Katti M. Ricke,289,Recruiter,Courtney Clark,test@gmail.com

Probably if you use pandas for to handle the csv could be more easy ..for install pandas just pip install pandas可能如果你使用 pandas 来处理 csv 可能会更容易..for install pandas just pip install pandas

import pandas as pd
import io
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# read csv using pandas and then convert the csv to html string ....this code later will add into body
str_io = io.StringIO()
df = pd.read_csv('test.csv')
df.to_html(buf=str_io)
table_html = str_io.getvalue()
print(table_html)


sender_email = "mymail@gmail.com"
receiver_email = "anothermail@gmail.com"
password = "mypass"

message = MIMEMultipart("alternative")
message["Subject"] = "Subject: Your Title"
message["From"] = sender_email
message["To"] = receiver_email

text = """\
Subject: Your Title"""

html = """\
<html>
  <body>
    <p>{table_html}</p>
  </body>
</html>
""".format(table_html=table_html)

part1 = MIMEText(text, "plain")
part2 = MIMEText(html, "html")
message.attach(part1)
message.attach(part2)

# Send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(
        sender_email, receiver_email, message.as_string()
    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM