久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python 發送郵件的四種方法匯總

瀏覽:2日期:2022-07-03 14:07:08

這里針對smtplib做了一系列封裝,可以完成以下四種場景:

發送純文本的郵件 發送html頁面的郵件 發送帶附件文件的郵件 發送能展示圖片的郵件

以上四種場景,已經做好了二次封裝,經測試OK,使用時直接傳入對應參數即可,直接上代碼

import smtplibfrom email.mime.text import MIMETextfrom email.mime.image import MIMEImagefrom email.mime.application import MIMEApplicationfrom email.mime.multipart import MIMEMultipartclass SendEMail(object): '''封裝發送郵件類''' 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) # 發送方郵箱賬號和授權碼 self.smtp_s.login(user=msg_from, password=pwd) def send_text(self, to_user, content, subject, content_type=’plain’): ''' 發送文本郵件 :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’): ''' 發送帶文件的郵件 :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’): ’’’ 發送帶圖片的郵件 :param to_user: 對方郵箱 :param subject: 郵件主題 :param content: 郵件正文 :param filename: 圖片路徑 :param content_type: 內容格式 ’’’ subject = subject msg = MIMEMultipart(’related’) # Html正文必須包含<img src='cid:imageid' alt='imageid' 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 發送郵件的四種方法匯總的詳細內容,更多關于python 發送郵件的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 天全县| 永丰县| 文登市| 沙河市| 东明县| 华池县| 永寿县| 和硕县| 江川县| 惠东县| 仪征市| 界首市| 延庆县| 潜江市| 开远市| 如东县| 达日县| 卢氏县| 金秀| 威远县| 隆化县| 昌都县| 安泽县| 广州市| 瓮安县| 临猗县| 阳春市| 铁岭县| 玛纳斯县| 林西县| 横峰县| 章丘市| 沽源县| 神木县| 阿荣旗| 筠连县| 弥勒县| 丰镇市| 石河子市| 崇礼县| 抚远县|