使用python完成郵件的發送html
一、郵件須要的基本信息python
二、python發送郵件服務器
一、郵件須要的基本信息app
發件人的服務器、發送人、接收人(是否多個)、主題、內容,是否有附件測試
二、python發送郵件ui
import smtplib
from email.mime.text import MIMEText #此模塊能夠用於發送正文
from email.mime.multipart import MIMEMultipart #此模塊用於發送帶附件的郵件
#郵件基礎信息配置
smtpserver = "smtp.163.com" #發件服務器,qq郵箱爲smtp.qq.com
port = 0 #端口,qq郵箱爲465
sender = "XX@163.com" #發件箱
psw = " XXXX" #qq郵箱爲受權碼
receiver = "XXXXXX@qq.com" #收件箱,多個郵箱爲["XX@qq.com","XXXX@163.com"]
msg=MIMEMultipart()
msg["from"]=sender
msg["to"]=receiver #發給單個郵箱
#msg["to"]=";".join(receiver) #發給多個郵箱
msg["subject"]="test"
body=MIMEText(mail_body,"html","utf-8")
msg.attach(body) #讀取附件中的正文做爲郵件正文
#附件信息的配置
file_path=r"D:\result.html" #讀取本地的一個一個測試報告
with open(file_path,"rb") as fp:
mail_body=fp.read()
att=MIMEText(mail_body,"basse64","utf-8")
att["Content-Type"]="application/octet-stream"
att["Content-Disposition"]='attachment; filename="test_report.html"' #將系統中的result.html重命名一下做爲附件發送
msg.attach(att)
#兼容通常郵箱和qq郵箱
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver) #鏈接服務器 ,通常郵箱,如163等
smtp.login(sender,psw) #登陸
except:
smtp=smtplib.SMTP_SSL(smtpserver,port) #鏈接服務器,qq郵箱
smtp.login(sender,psw) #登陸
smtp.sendmail(sender,receiver,msg.as_string()) #發送
smtp.quit() #關閉