1 import  smtplib
 2 from email.mime.text import MIMEText
 3 from email.mime.multipart import MIMEMultipart
 4 """多用户及带附件发送邮件代码"""
 5 
 6 smtpserver = 'smtp.163.com' #发送邮箱服务器
 7 
 8 user = '***@163.com'
 9 password = '密码'
10 
11 sender = 'a5974939632@163.com'
12 receive = ['接收邮箱1', '接收邮箱2']
13 
14 subject = 'Web Selenium 自动化测试报告'
15 content = '<html><h1 style="color:red">我要自学网,自学成才!</h1></html>'
16 
17 send_file = open(r'E:\study\SeleniumPython\UnitTest\Test_Baidu\test_report\2019-01-08 12_00_24result.html','rb').read()
18 
19 att = MIMEText(send_file, 'base64', 'utf-8')
20 att['Content-Type'] = 'application/octet-stream'
21 att['Content-Disposition'] = 'attachment:filename="2019-01-08 12_00_24result.html"'
22 
23 msgRoot = MIMEMultipart()
24 msgRoot.attach(MIMEText(content, 'html', 'utf-8'))
25 msgRoot['Subject'] = subject
26 msgRoot['From'] = sender
27 msgRoot['To'] = ','.join(receive)
28 msgRoot.attach(att)
29 
30 smtp = smtplib.SMTP_SSL(smtpserver,465)
31 smtp.helo(smtpserver)
32 smtp.ehlo(smtpserver)
33 smtp.login(user,password)
34 
35 print("start send Email...")
36 smtp.sendmail(sender,receive,msgRoot.as_string())
37 smtp.quit()
38 print("send email end!")

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。