欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用python SMTP进行邮件发送

程序员文章站 2022-08-13 19:10:40
1 import smtplib 2 from email.mime.text import MIMEText 3 from email.mime.multipart import MIMEMultipart 4 """多用户及带附件发送邮件代码""" 5 6 smtpserver = 'smtp.... ......
 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!")