如何在多部分中随身发送电子邮件?

时间:2019-06-17 19:18:48

标签: python-3.x

我想发送带有附件和正文的电子邮件,但是无法发送正文内容。

我并使用MIMEMultipart发送带有附件的电子邮件,但不确定如何也发送正文部分。

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


TO = ['destination@domain.com']
FROM = 'DoNotReply-PythonApp@libertymutual.com'
SUBJECT = 'Atarai Details'

fileToSend = "Atarai_v1.0.txt"

msg = "Hi There\n Please find the attachement for reference."
msg = MIMEMultipart(msg)
msg["From"] = FROM
msg["To"] = ",".join(TO)
msg["Subject"] = SUBJECT


fp = open(fileToSend)
attachment = MIMEText(fp.read())
fp.close()

attachment.add_header("Content-Disposition", "attachment", 
filename=fileToSend)
msg.attach(attachment)

server = smtplib.SMTP('Myrelayserver.com')
server.sendmail(FROM, TO, msg.as_string())
server.quit()

我期望的电子邮件应具有附件文件以及正文消息

嗨,那里

请找到附件以供参考。

0 个答案:

没有答案