python實(shí)現(xiàn)發(fā)送帶附件的郵件代碼分享
具體代碼如下:
from django.template import loaderfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.header import Headerimport smtplibimport tracebackclass SendEmail(object): ''' 發(fā)送html郵件 ''' def __init__(self, mail_host, mail_port, mail_user, mail_pass, sender, to_list_email):# 創(chuàng)建郵件對(duì)象self.msg = MIMEMultipart()# 郵箱服務(wù)地址self.mail_host = mail_host# 郵箱端口號(hào)self.mail_port = mail_port# 郵箱賬號(hào)self.mail_user = mail_user# 密碼self.mail_pass = mail_pass# 發(fā)送人self.sender = sender# 收件人郵箱列表self.to_list_email = to_list_email def make_html(self, base_html_path, **kwargs):''':param base_html_path: html模板文件路徑:param **kwargs: 模板中的參數(shù):return:'''mail_html = loader.render_to_string( template_name=base_html_path, context={# 'id': tid,**kwargs # 傳入模板文件的數(shù)據(jù) })return mail_html def add_attachment(self, file_path):'''制作附件:param file_path::return:'''with open(file_path, ’rb’) as f: content = f.read()att = MIMEText(content, _subtype=’plain’, _charset=’utf-8’)att['Content-Type'] = ’application/octet-stream’att['Content-Disposition'] = ’attachment; filename=task_report.docx’att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '{}'.format(filename))) # 如果文件名中有中文的話需設(shè)置return att def send_html_email(self, base_html_path, subject, str_to, str_cc, file_path, **kwargs):''':param html: html文件對(duì)象:param subject: 郵件主題:return:'''html = self.make_html(base_html_path, **kwargs)self.msg.attach(MIMEText(str(html), ’html’))self.msg[’from’] = Header(’安全測(cè)試平臺(tái)’, ’utf-8’)self.msg[’Subject’] = Header(subject, ’utf-8’)self.msg['Accept-Language'] = 'zh-CN'self.msg['Accept-Charset'] = 'ISO-8859-1,utf-8'self.msg[’to’] = str_to # 發(fā)送人 strself.msg[’cc’] = str_cc # 抄送人 str# 添加附件att = self.add_attachment(file_path)self.msg.attach(att)# 發(fā)送郵件try: server = smtplib.SMTP() server.connect(self.mail_host, self.mail_port) server.login(self.mail_user, self.mail_pass) server.sendmail(self.sender, self.to_list_email, self.msg.as_string()) server.quit()except Exception: print(traceback.format_exc())
內(nèi)容擴(kuò)展:
實(shí)例二:
import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.application import MIMEApplication'''發(fā)送帶附件郵件的完整代碼'''class HandleEmail(): def handle_email(self): # step1 連接smtp服務(wù)器并登陸 smtp = smtplib.SMTP_SSL(host=’smtp.qq.com’, port=465) smtp.login(user=’[email protected]’, password=’XXXXX’) # 構(gòu)造多組件郵件并完善郵件描述性信息 msg = MIMEMultipart() msg[’Subject’] = ’帶附件的郵件-01’ msg[’FROM’] = ’[email protected]’ msg[’To’] = ’[email protected]’ # 添加郵件的文本內(nèi)容 text = MIMEText(_text=’這是郵件正文的內(nèi)容’, _charset=’UTF8’) msg.attach(text) # 添加附件和附件header with open(file=r’XXXXXXXXreport.html’, mode=’rb’) as f: content = f.read() attachment = MIMEApplication(_data=content) attachment.add_header(’content-disposition’, ’attachment’, filename=’report.html’) msg.attach(attachment) # 發(fā)送郵件 smtp.send_message(msg=msg, from_addr=’[email protected]’, to_addrs=’[email protected]’)if __name__ == ’__main__’: e_mail = HandleEmail() e_mail.handle_email()
使用創(chuàng)建好的smtp對(duì)象發(fā)送郵件,需要把上面編輯好的msg作為參數(shù)傳入,然后填寫收發(fā)件人,如果有多個(gè)收件人,以列表的形式傳入?yún)?shù)
smtp.send_message(msg=msg,from_addr=’’,to_addrs=’’) # 單個(gè)收件人smtp.send_message(msg=msg,from_addr=’’,to_addrs=[’收件人一’,’收件人二’]) # 多個(gè)收件人
到此這篇關(guān)于python實(shí)現(xiàn)發(fā)送帶附件的郵件代碼分享的文章就介紹到這了,更多相關(guān)利用python實(shí)現(xiàn)發(fā)送帶附件的郵件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式2. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼4. php redis setnx分布式鎖簡(jiǎn)單原理解析5. 《Java程序員修煉之道》作者Ben Evans:保守的設(shè)計(jì)思想是Java的最大優(yōu)勢(shì)6. CSS3中Transition屬性詳解以及示例分享7. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程8. 如何在PHP中讀寫文件9. java加載屬性配置properties文件的方法10. 什么是Python變量作用域
