天天看点

python3发送邮件带附件_python3 发送邮件,主题+附件

import smtplib

from email.mime.text import MIMEText

from email.header import Header

from email.mime.image import MIMEImage

#发送图片

from email.mime.multipart import MIMEMultipart

sender = " XXX"

receiver = "XXX"

subject = 'python email test'

smtp_server = 'email server'

username = 'xxx'

password = 'xxx'

msg = MIMEMultipart()

msg['Subject'] = Header(subject, 'utf-8')

#msg['Subject']='邮件标题'

#text = MIMEText('Hello, python send this mail for testing',

'plain', 'utf-8')

#msg=MIMEText("要发送的内容","格式,例如:html,plain","编码,例如:gb2312,utf-8")

text = MIMEText("sdfsfsd")

msg.attach(text)

#构造附件

#att =

MIMEText(open("D:\python\writeFile.rar","rb").read(),'base64','utf-8')

att

=MIMEText(open("D:\python\writeFile.rar","rb").read(),'base64','utf-8')

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

att["Content-Disposition"] = 'attachment;filename =

"writeFile.rar"'

msg.attach(att)

smtp = smtplib.SMTP()

smtp.connect('smtp_server')

smtp.login(username,password)

smtp.sendmail(sender, receiver, msg.as_string())

#send  email

smtp.quit()

print('send mail successfully')