python之郵件發送自動化
# -*- coding:utf-8 -*-
#@Time : 2020/3/24 22:55
#@Autor: Mr.White
#@File : 發送郵件.py
一、導入所需要的類
1 import smtplib 2 import datetime 3 import time 4 from email.mime.text import MIMEText 5 from email.header import Header
二、服務配置
1 smtpserver='smtp.qq.com' 2 user='[email protected]' 3 password='jiayou2021' 4 sender='[email protected]' 5 #receive='[email protected]' 6 receive='[email protected]'
三、標題與內容
1 subject='python郵件自動化報表' 2 content='<html>今天是:{},寶寶出生的日子<h1 style="color:red">祝<br>寶寶生日快樂</h1><h1 style="color:orange">寶寶生日快樂</h1><h1 style="color:yellow">寶寶生日快樂</h1><h1 style="color:green">寶寶生日快樂</h1><h1 style="color:cyan">寶寶生日快樂</h1><h1 style="color:blue">寶寶生日快樂</h1><h1 style="color:purple">寶寶生日快樂</h1></html>'.format(datetime.datetime.now().strftime('%Y-%m-%d'))
四、組裝
1 msg=MIMEText(content,'html','utf-8') 2 msg['Subject']=Header(subject,'utf-8') 3 msg['From']=sender 4 msg['To']=receive
五、設置發送與返回
1 smtp=smtplib.SMTP_SSL(smtpserver,465) 2 smtp.helo(smtpserver) 3 smtp.ehlo(smtpserver)
六、登陸、發件、日誌
1 smtp.login(user,password) 2 print("start send email...") 3 smtp.sendmail(sender,receive,msg.as_string()) 4 smtp.quit() 5 print("send eamil end!")
七、預覽結果