使用身体的csv数据发送带有python的电子邮件 [英] Sending an email with python using csv data for the body

查看:108
本文介绍了使用身体的csv数据发送带有python的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用csv库将数据提取到电子邮件正文中.我正在从csv中提取某些列作为正文.我正在使用垃圾gmail帐户进行测试.我只是对如何使用for循环感到困惑.如果我是对的,则需要一个for循环来读取行,然后需要一个for循环以电子邮件发送出去.我确实读过读写CSV文件,但是不确定如何使用smtplib.

I am using the csv library to pull data to into an email body. I am pulling certain columns from the csv for the body. I am using a junk gmail account to test. I'm just confused on how to use the for loop. If I'm correct, you need a for loop to read the rows then a for loop to email out. I did read Reading and Writing CSV Files but was uncertain how to implement it with 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文件:

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

推荐答案

如果您使用熊猫来处理csv,可能会更容易..对于安装熊猫,只需pip install pandas

Probably if you use pandas for to handle the csv could be more easy ..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()
    )

这篇关于使用身体的csv数据发送带有python的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆