python 發(fā)送郵件的四種方法匯總
這里針對(duì)smtplib做了一系列封裝,可以完成以下四種場(chǎng)景:
- 發(fā)送純文本的郵件
- 發(fā)送html頁(yè)面的郵件
- 發(fā)送帶附件文件的郵件
- 發(fā)送能展示圖片的郵件
以上四種場(chǎng)景,已經(jīng)做好了二次封裝,經(jīng)測(cè)試OK,使用時(shí)直接傳入對(duì)應(yīng)參數(shù)即可,直接上代碼
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
class SendEMail(object):
"""封裝發(fā)送郵件類"""
def __init__(self, host, port, msg_from, pwd):
self.msg_from = msg_from
self.password = pwd
# 郵箱服務(wù)器地址和端口
self.smtp_s = smtplib.SMTP_SSL(host=host, port=port)
# 發(fā)送方郵箱賬號(hào)和授權(quán)碼
self.smtp_s.login(user=msg_from, password=pwd)
def send_text(self, to_user, content, subject, content_type='plain'):
"""
發(fā)送文本郵件
:param to_user: 對(duì)方郵箱
:param content: 郵件正文
:param subject: 郵件主題
:param content_type: 內(nèi)容格式:'plain' or 'html'
:return:
"""
msg = MIMEText(content, _subtype=content_type, _charset="utf8")
msg["From"] = self.msg_from
msg["To"] = to_user
msg["subject"] = subject
self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)
def send_file(self, to_user, content, subject, reports_path, filename, content_type='plain'):
"""
發(fā)送帶文件的郵件
:param to_user: 對(duì)方郵箱
:param content: 郵件正文
:param subject: 郵件主題
:param reports_path: 文件路徑
:param filename: 郵件中顯示的文件名稱
:param content_type: 內(nèi)容格式
"""
file_content = open(reports_path, "rb").read()
msg = MIMEMultipart()
text_msg = MIMEText(content, _subtype=content_type, _charset="utf8")
msg.attach(text_msg)
file_msg = MIMEApplication(file_content)
file_msg.add_header('content-disposition', 'attachment', filename=filename)
msg.attach(file_msg)
msg["From"] = self.msg_from
msg["To"] = to_user
msg["subject"] = subject
self.smtp_s.send_message(msg, from_addr=self.msg_from, to_addrs=to_user)
def send_img(self, to_user, subject, content, filename, content_type='html'):
'''
發(fā)送帶圖片的郵件
:param to_user: 對(duì)方郵箱
:param subject: 郵件主題
:param content: 郵件正文
:param filename: 圖片路徑
:param content_type: 內(nèi)容格式
'''
subject = subject
msg = MIMEMultipart('related')
# Html正文必須包含<img src="cid:imageid" alt="imageid" width="100%" height="100%>
content = MIMEText(content, _subtype=content_type, _charset="utf8")
msg.attach(content)
msg['Subject'] = subject
msg['From'] = self.msg_from
msg['To'] = to_user
with open(filename, "rb") as file:
img_data = file.read()
img = MIMEImage(img_data)
img.add_header('Content-ID', 'imageid')
msg.attach(img)
self.smtp_s.sendmail(self.msg_from, to_user, msg.as_string())
以上就是python 發(fā)送郵件的四種方法匯總的詳細(xì)內(nèi)容,更多關(guān)于python 發(fā)送郵件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python如何獲得百度統(tǒng)計(jì)API的數(shù)據(jù)并發(fā)送郵件示例代碼
- Python基于SMTP發(fā)送郵件的方法
- python實(shí)現(xiàn)發(fā)送郵件
- python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警
- python自動(dòng)化發(fā)送郵件實(shí)例講解
- python實(shí)現(xiàn)定時(shí)發(fā)送郵件到指定郵箱
- python實(shí)現(xiàn)定時(shí)發(fā)送郵件
- python腳本定時(shí)發(fā)送郵件
- python 發(fā)送郵件的示例代碼(Python2/3都可以直接使用)
- 詳解python定時(shí)簡(jiǎn)單爬取網(wǎng)頁(yè)新聞存入數(shù)據(jù)庫(kù)并發(fā)送郵件
- Python發(fā)送郵件實(shí)現(xiàn)基礎(chǔ)解析
- Python 調(diào)用API發(fā)送郵件
相關(guān)文章
解決Keras自帶數(shù)據(jù)集與預(yù)訓(xùn)練model下載太慢問(wèn)題
這篇文章主要介紹了解決Keras自帶數(shù)據(jù)集與預(yù)訓(xùn)練model下載太慢問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
PyTorch中 tensor.detach() 和 tensor.data 的區(qū)別詳解
今天小編就為大家分享一篇PyTorch中 tensor.detach() 和 tensor.data 的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
Python統(tǒng)計(jì)中文詞頻的四種方法小結(jié)
統(tǒng)計(jì)中文詞頻是Python考試中常見的操作,本文我們總結(jié)了四種常見的中文詞頻統(tǒng)計(jì)方法,并列出代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
FastApi如何快速構(gòu)建一個(gè)web項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了FastApi如何快速構(gòu)建一個(gè)web項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python循環(huán)語(yǔ)句中else的用法總結(jié)
這篇文章給大家整理了關(guān)于Python中循環(huán)語(yǔ)句中else的用法,包括常規(guī)的 if else 用法、if else 快捷用法、與 for 關(guān)鍵字一起用、與 while 關(guān)鍵字一起用以及與 try except 一起用的用法總結(jié),有需要的朋友們可以參考借鑒。2016-09-09
Python實(shí)現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)
這篇文章主要實(shí)現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組、堆棧、隊(duì)列、鏈表。大家都知道可以用C語(yǔ)言實(shí)現(xiàn)這幾種數(shù)據(jù)結(jié)構(gòu),其實(shí)Python也可以實(shí)現(xiàn),下面跟著小編一起來(lái)學(xué)習(xí)。2016-08-08
Python抓取通過(guò)Ajax加載數(shù)據(jù)的示例
在網(wǎng)頁(yè)上,有一些內(nèi)容是通過(guò)執(zhí)行Ajax請(qǐng)求動(dòng)態(tài)加載數(shù)據(jù)渲染出來(lái)的,本文主要介紹了使用Python抓取通過(guò)Ajax加載數(shù)據(jù),感興趣的可以了解一下2023-05-05
python 根據(jù)時(shí)間來(lái)生成唯一的字符串方法
今天小編就為大家分享一篇python 根據(jù)時(shí)間來(lái)生成唯一的字符串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
如何利用Tensorflow2進(jìn)行貓狗分類識(shí)別
圖像分類是根據(jù)圖像的語(yǔ)義信息將不同類別圖像區(qū)分開來(lái),是計(jì)算機(jī)視覺(jué)中重要的基本問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于如何利用Tensorflow2進(jìn)行貓狗分類識(shí)別的相關(guān)資料,需要的朋友可以參考下2022-06-06

