我們平時需要使用 Python 發送各類郵件,這個需求怎么來實作?答案其實很簡單,smtplib 和 email庫可以幫忙實作這個需求,smtplib 和 email 的組合可以用來發送各類郵件:普通文本,HTML 形式,帶附件,群發郵件,帶圖片的郵件等等,我們這里將會分幾節把發送郵件功能解釋完成,
smtplib 是 Python 用來發送郵件的模塊,email 是用來處理郵件訊息,
群發郵件的時候需要注意收件人(receiver)的值,它為串列形式:
import smtplib
from email.mime.text import MIMEText
sender = '***'
receiver = ['***', '***', '...', '***']
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
msg = MIMEText('你好', 'plain', 'utf-8')
msg['Subject'] = subject
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
注意:這里的代碼并沒有把例外處理加入,需要讀者自己處理例外,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/189676.html
標籤:其他
上一篇:re模塊常用方法
