我正在使用 csv 库将数据提取到电子邮件正文中。我正在从 csv 中提取正文的某些列。我正在使用垃圾 Gmail 帐户进行测试。我只是对如何使用 for 循环感到困惑。如果我是正确的,您需要一个 for 循环来读取行,然后需要一个 for 循环来通过电子邮件发送。我确实读过读取和写入 CSV 文件但不确定如何用 smtplib 来实现它。
import csv, smtplib, ssl
message = """Subject: Your Title
{supervisor} {title} {start}"""
from_address = "[email protected]"
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 文件:
StartDate,Employee Name,PC,Title,Supervisor Name,email
12/9/2019,Katti M. Ricke,289,Recruiter,Courtney Clark,[email protected]