python3发邮件怎么写正文

Python022

python3发邮件怎么写正文,第1张

以下就已经构造了一个完整的邮件了,写的应该算清楚了, 有问题继续追问

from email.mime.text import MIMEText

m = MIMEText("消息正文",_charset='utf8')

m['Subject'] = "标题"

m['To'] = "发给谁"

html ='''你要发送的内容'''+var1+'''其他部分的内容'''+var2+'''剩下的内容'''

msg.attach(MIMEText(html, 'html'))

from email.Header import Header

from email.MIMEText import MIMEText

from email.MIMEMultipart import MIMEMultipart

import smtplib, datetime

#创建一个带附件的实例

msg = MIMEMultipart()

#构造附件

att = MIMEText(open('d:\\tc201.rar', 'rb').read(), 'base64', 'gb2312')

att["Content-Type"] = 'application/octet-stream'

att["Content-Disposition"] = 'attachment filename="tc201.rar"'

msg.attach(att)

#加邮件头

msg['to'] = '[email protected]'

msg['from'] = '[email protected]'

msg['subject'] = Header('冒烟测试结果 (' + str(datetime.date.today()) + ')', \

                        'gb2312')

#发送邮件

server = smtplib.SMTP('smtp.xxx.com')

server.sendmail(msg['from'], msg['to'], \

                msg.as_string())

server.close