smtp是傳送郵件的協議,python內建對smtp的支援(smtplib模組和email模組),可以傳送純文字郵件、html郵件以及帶附件的郵件
python的smtplib提供了一種很方便的途徑傳遞電子郵件,對smtp進行了簡單的封裝
傳送純文字郵件
#匯入smtplib模組
from smtplib import smtp
from email.mime.text import mimetext
from email.header import header
def send_email(smtp_host, from_addr, password, to_addrs, subject, content):
email_client = smtp(smtp_host)
email_client.login(from_addr, password)
msg = mimetext(content,'plain','utf-8')
msg['subject'] = header(subject, 'utf-8')#subject
msg['from'] = '[email protected]'
msg['to'] = "[email protected]"
#sendmail(郵件傳送者位址,字串列表(收件人),傳送內容)
email_client.sendmail(from_addr, to_addrs, msg.as_string())
send_email("smtp.163.com","[email protected]","password","[email protected]","lsllsldld","hellow")
傳送html格式的郵件
#匯入smtplib模組
from smtplib import smtp
from email.mime.text import mimetext
from email.header import header
def send_email(smtp_host, from_addr, password, to_addrs, subject, content):
email_client = smtp(smtp_host)
email_client.login(from_addr, password)
#傳送純文字郵件 設定為plain,傳送html郵件修改為 mimetext(content,'html','utf-8')
msg = mimetext(content,'html','utf-8')
msg['subject'] = header(subject, 'utf-8')#subject
msg['from'] = '[email protected]'
msg['to'] = "[email protected]"
#sendmail(郵件傳送者位址,字串列表(收件人),傳送內容)
#傳送純文字郵件
email_client.sendmail(from_addr, to_addrs, msg.as_string())
send_email("smtp.163.com","***@163.com","password","***@qq.com","lsllsldld","
")
Python傳送電子郵件
源 sendemail.py import smtplib from email.mime.text import mimetext msg mimetext the body of the email is here 這裡是你的信件中的內容 msg from ltoddy 163.com 這裡是傳...
python 傳送電子郵件
from smtplib import smtp from email.header import header from email.mime.text import mimetext 傳送郵件 def send email 請自行修改下面的郵件傳送者和接收者 sender abcdefg 126...
電子郵件(二)
flask mail中文文件 配置 flask mail 傳送郵件 大量郵件 附件 單元測試以及禁止傳送郵件 傳送郵件 為了能夠傳送郵件,首先需要建立乙個 message 例項 from flask mail import message defindex msg message hello sen...