python 發(fā)送郵件的四種方法匯總
更新時間:2020年12月02日 11:46:44 作者:小公瑾
這篇文章主要介紹了python 發(fā)送郵件的四種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
這里針對smtplib做了一系列封裝,可以完成以下四種場景:
- 發(fā)送純文本的郵件
- 發(fā)送html頁面的郵件
- 發(fā)送帶附件文件的郵件
- 發(fā)送能展示圖片的郵件
以上四種場景,已經(jīng)做好了二次封裝,經(jīng)測試OK,使用時直接傳入對應參數(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 # 郵箱服務器地址和端口 self.smtp_s = smtplib.SMTP_SSL(host=host, port=port) # 發(fā)送方郵箱賬號和授權碼 self.smtp_s.login(user=msg_from, password=pwd) def send_text(self, to_user, content, subject, content_type='plain'): """ 發(fā)送文本郵件 :param to_user: 對方郵箱 :param content: 郵件正文 :param subject: 郵件主題 :param content_type: 內容格式:'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: 對方郵箱 :param content: 郵件正文 :param subject: 郵件主題 :param reports_path: 文件路徑 :param filename: 郵件中顯示的文件名稱 :param content_type: 內容格式 """ 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: 對方郵箱 :param subject: 郵件主題 :param content: 郵件正文 :param filename: 圖片路徑 :param content_type: 內容格式 ''' 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ā)送郵件的四種方法匯總的詳細內容,更多關于python 發(fā)送郵件的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:
- Python如何獲得百度統(tǒng)計API的數(shù)據(jù)并發(fā)送郵件示例代碼
- Python基于SMTP發(fā)送郵件的方法
- python實現(xiàn)發(fā)送郵件
- python使用Windows的wmic命令監(jiān)控文件運行狀況,如有異常發(fā)送郵件報警
- python自動化發(fā)送郵件實例講解
- python實現(xiàn)定時發(fā)送郵件到指定郵箱
- python實現(xiàn)定時發(fā)送郵件
- python腳本定時發(fā)送郵件
- python 發(fā)送郵件的示例代碼(Python2/3都可以直接使用)
- 詳解python定時簡單爬取網(wǎng)頁新聞存入數(shù)據(jù)庫并發(fā)送郵件
- Python發(fā)送郵件實現(xiàn)基礎解析
- Python 調用API發(fā)送郵件
相關文章
python數(shù)據(jù)結構之圖的實現(xiàn)方法
這篇文章主要介紹了python數(shù)據(jù)結構之圖的實現(xiàn)方法,實例分析了Python圖的表示方法與常用尋路算法的實現(xiàn)技巧,需要的朋友可以參考下2015-07-07Pandas使用Merge與Join和Concat分別進行合并數(shù)據(jù)效率對比分析
這篇文章主要給大家介紹了關于pandas中DataFrame數(shù)據(jù)合并連接(merge、join、concat)的相關資料,文中介紹的非常詳細,需要的朋友可以參考下2022-12-12Python?Serial串口的簡單數(shù)據(jù)收發(fā)方式
這篇文章主要介紹了Python?Serial串口的簡單數(shù)據(jù)收發(fā)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09解決Python?出現(xiàn)File?“<stdin>“,?line?1非語法錯誤的問題
這篇文章主要介紹了Python?出現(xiàn)File?“<stdin>“,?line?1非語法錯誤的解決辦法,本文給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03python?Opencv實現(xiàn)停車位識別思路詳解
這篇文章主要介紹了Opencv實現(xiàn)停車位識別,本文通過示例代碼場景分析給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07