使用python3发送包含多个csv文件的电子邮件

时间:2018-10-03 19:54:35

标签: python-3.x

我正在编写一个python脚本来发送带有两个csv文件作为附件的电子邮件。 在Google中进行大量发现后,我可以得到以下代码,但它不会向我的收件箱发送任何电子邮件。 有人可以帮助我如何编辑代码,以确保我可以发送一封包含所有文件作为附件的电子邮件。

我当前的代码:

import smtplib
from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email.mime.base import MIMEBase

from email import encoders

path = 'C:/Users/kasar/PycharmProjects/Cisco_Fabric/'

file = ['input.csv','output.csv']

def emailfiles(file):

    emailLogin = "exchgen-group24771"
    emailPassword = 'passwd'
    fromaddr = "from@.com"
    toaddr = "to@.com"
    body = "TEXT YOU WANT TO SEND"
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = toaddr
    msg['Subject'] = "SUBJECT OF THE EMAIL"
    msg.attach(MIMEText(body, 'plain'))
    for i in file:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(path + i , 'rb').read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', "attachment; filename= %s" %i)
        msg.attach(part)
        smtpServerName = 'address'
        server = smtplib.SMTP(smtpServerName, 25)
        server.starttls()
        server.login(emailLogin,emailPassword)
        text = msg.as_string()
        server.sendmail(fromaddr, toaddr, text)
        server.quit()

0 个答案:

没有答案