700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python3.5 email发送邮件 包含txt 图片 HTML 附件

Python3.5 email发送邮件 包含txt 图片 HTML 附件

时间:2020-10-06 20:15:38

相关推荐

Python3.5 email发送邮件 包含txt 图片 HTML 附件

直接套用代码即可

from email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipartfrom email import encodersimport smtplibimport timedef send_mail(subject):email_host = '' # 服务器地址sender = '' # 发件人password = '' # 密码,如果是授权码就填授权码receiver = '' # 收件人msg = MIMEMultipart()msg['Subject'] = subject # 标题msg['From'] = '' # 发件人昵称msg['To'] = '' # 收件人昵称signature = '''\n\t this is auto test report!\n\t you don't need to follow'''# text = MIMEText(signature, 'plain') # 签名# msg.attach(text)# 正文-图片 只能通过html格式来放图片,所以要注释25,26行mail_msg = '''<p>\n\t this is auto test report!</p><p>\n\t you don't need to follow</p><p><a href="/wjoxoxoxxx">我的博客:</a></p><p>截图如下:</p><p><img src="cid:image1"></p>'''msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))# 指定图片为当前目录fp = open(r'111.jpg', 'rb')msgImage = MIMEImage(fp.read())fp.close()# 定义图片 ID,在 HTML 文本中引用msgImage.add_header('Content-ID', '<image1>')msg.attach(msgImage)ctype = 'application/octet-stream'maintype, subtype = ctype.split('/', 1)# 附件-图片image = MIMEImage(open(r'111.jpg', 'rb').read(), _subtype=subtype)image.add_header('Content-Disposition', 'attachment', filename='img.jpg')msg.attach(image)# 附件-文件file = MIMEBase(maintype, subtype)file.set_payload(open(r'320k.txt', 'rb').read())file.add_header('Content-Disposition', 'attachment', filename='test.txt')encoders.encode_base64(file)msg.attach(file)# 发送smtp = smtplib.SMTP()smtp.connect(email_host, 25)smtp.login(sender, password)smtp.sendmail(sender, receiver, msg.as_string())smtp.quit()print('success')if __name_- == '__main__':now = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))subject = now + '自动化测试报告'send_mail(subject)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。