用python SMTP进行邮件发送

python学习网 2019-01-14 12:07:01
 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 = 'a5974939632@163.com'
 9 password = 'wang1989'
10 
11 sender = 'a5974939632@163.com'
12 receive = ['a5974939632@126.com', 'a5974939632@163.com']
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!")

 

阅读(5004) 评论(0)