如何使用MIME / SMTP和电子邮件附加CSV文件?

时间:2017-04-04 18:14:36

标签: python csv email smtp mime

我正在尝试附加CSV文件并通过电子邮件发送。

目前,我正在执行以下操作,但它只是附加一个空的CSV文件,而不是附加我在同一目录中的CSV文件:

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE
from email import encoders


def email_attachment():
    SUBJECT = 'Subject string'

    msg = MIMEMultipart()
    msg['From'] = 'my_email@yahoo.com'
    msg['To'] = COMMASPACE.join(['recepient_email@gmail.com'])
    msg['Subject'] = SUBJECT

    part = MIMEBase('application', "octet-stream")
    #    I have a CSV file named `attachthisfile.csv` in the same directory that I'd like to attach and email
    part.set_payload(open("./attachthisfile.csv", "rb").read())
    encoders.encode_base64(part)

    part.add_header('Content-Disposition', 'attachment', filename='attachthisfile.csv')

    msg.attach(part)

    smtpObj = smtplib.SMTP('smtp.mail.yahoo.com', 587)
    smtpObj.ehlo()
    smtpObj.starttls()
    smtpObj.login('my_email@yahoo.com', 'myemailpassword')
    smtpObj.sendmail('my_email@yahoo.com', 'recepient_email@gmail.com', msg.as_string())

    smtpObj.quit()

所以我的问题是,我能做错什么?如何将CSV文件附加到同一目录和电子邮件中,而不是创建一个空的CSV文件并将其命名为同一个,并通过电子邮件发送?

2 个答案:

答案 0 :(得分:4)

问题似乎与这一行有关:

part.add_header('Content-Disposition', 'attachment; filename=“attachthisfile.csv"')

它在filename=之后包含非ASCII字符'\ xe2'。这是旧的ascii encoding问题,在这种情况下是: vs "

作为旁注,您还可以更改 add_header 参数以匹配此Python docs example

part.add_header('Content-Disposition', 'attachment', filename='attachthisfile.csv')

完整的工作示例:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE
from email import encoders

SUBJECT = 'Subject string'
FILENAME = 'attachthisfile.csv'
FILEPATH = './attachthisfile.csv'
MY_EMAIL = 'example@yahoo.com'
MY_PASSWORD = '********************'
TO_EMAIL = 'example@gmail.com'
SMTP_SERVER = 'smtp.mail.yahoo.com'
SMTP_PORT = 587

msg = MIMEMultipart()
msg['From'] = MY_EMAIL
msg['To'] = COMMASPACE.join([TO_EMAIL])
msg['Subject'] = SUBJECT

part = MIMEBase('application', "octet-stream")
part.set_payload(open(FILEPATH, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=FILENAME)  # or
# part.add_header('Content-Disposition', 'attachment; filename="attachthisfile.csv"')
msg.attach(part)

smtpObj = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(MY_EMAIL, MY_PASSWORD)
smtpObj.sendmail(MY_EMAIL, TO_EMAIL, msg.as_string())
smtpObj.quit()

编辑:添加完整示例

答案 1 :(得分:0)

  

<强>问题即可。所以我的问题是,我可能做错了什么?

你的代码对我有用,没有错。

请编辑您的问题并添加以下输出:

def environ():
    import os
    for f in os.listdir('.'):
        print('%s\tuid=%s,\tgid=%s,\tsize=%s' % (f, os.stat(f)[4], os.stat(f)[5], os.stat(f)[6])  )  

请务必从同一*.py个文件和同一def email_attachment()

运行该文件
def email_attachment():
    environ()
    #...