1. 程式人生 > >傳送郵件(多人)

傳送郵件(多人)

#SMTP:簡單郵件傳輸協議,屬於TCP/IP協議
#smtplib模組,傳送郵件
#email模組,負責構造郵件
import smtplib #傳送郵件模組
from email.mime.text import MIMEText #定義郵件內容
from email.header import Header #定義郵件標題
from email.mime.multipart import MIMEMultipart #定義郵件附件
#傳送郵箱伺服器
smtpserver='smtp.qq.com'
#設定使用者名稱和密碼
user='****@qq.com'
password = '****' #指的是授權密碼不是登入密碼
#傳送和接收郵箱
sender='***@qq.com'
receive="***@126.com,****@qq.com" #多個收件人
#傳送郵件主題和內容
subject="Selenium 自動化測試"
content='<html><h1 style="color:red">我要自學selenium,努力加油堅持</h></html>'
#定義HTML郵件正文
msg=MIMEText(content,'html','utf-8')
msg["Subject"]=Header(subject,'utf-8')
msg['From']='****@qq.com'
msg['To']='****@126.com'

#ssl協議埠號是465、587
smtp=smtplib.SMTP_SSL(smtpserver,465)
#向伺服器標識使用者身份
smtp.helo(smtpserver)
#伺服器返回結果卻認
smtp.ehlo(smtpserver)
#登入郵箱伺服器使用者名稱和密碼
smtp.login(user,password)

print('Start send Email...')
#傳送,傳送多人要用到split分割成列表
smtp.sendmail(sender,receive.split(','),msg.as_string())
smtp.quit()
print('Send Email end!')