1. 程式人生 > >用python SMTP進行郵件傳送

用python 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 = '[email protected]'
 9 password = 'wang1989'
10 
11 sender = '[email protected]'
12 receive = ['[email protected]
', '[email protected]'] 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!")