发送包含适当MIME类型的多部分电子邮件。
https://docs.python.org/2/library/email-
examples.html
所以可能是这样的(我测试过):
import smtplibimport mimetypesfrom email.mime.multipart import MIMEMultipartfrom email import enprersfrom email.message import Messagefrom email.mime.audio import MIMEAudiofrom email.mime.base import MIMEbasefrom email.mime.image import MIMEImagefrom email.mime.text import MIMETextemailfrom = "sender@example.com"emailto = "destination@example.com"fileToSend = "hi.csv"username = "user"password = "password"msg = MIMEMultipart()msg["From"] = emailfrommsg["To"] = emailtomsg["Subject"] = "help I cannot send an attachment to save my life"msg.preamble = "help I cannot send an attachment to save my life"ctype, encoding = mimetypes.guess_type(fileToSend)if ctype is None or encoding is not None: ctype = "application/octet-stream"maintype, subtype = ctype.split("/", 1)if maintype == "text": fp = open(fileToSend) # Note: we should handle calculating the charset attachment = MIMEText(fp.read(), _subtype=subtype) fp.close()elif maintype == "image": fp = open(fileToSend, "rb") attachment = MIMEImage(fp.read(), _subtype=subtype) fp.close()elif maintype == "audio": fp = open(fileToSend, "rb") attachment = MIMEAudio(fp.read(), _subtype=subtype) fp.close()else: fp = open(fileToSend, "rb") attachment = MIMEbase(maintype, subtype) attachment.set_payload(fp.read()) fp.close() enprers.enpre_base64(attachment)attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)msg.attach(attachment)server = smtplib.SMTP("smtp.gmail.com:587")server.starttls()server.login(username,password)server.sendmail(emailfrom, emailto, msg.as_string())server.quit()

![如何使用Python发送带有.csv附件的电子邮件[重复] 如何使用Python发送带有.csv附件的电子邮件[重复]](http://www.mshxw.com/aiimages/31/648869.png)
