這是蒐羅網絡上的文章總結的一份文檔,python
參考:服務器
https://www.jb51.net/article/140604.htm網絡
https://www.jb51.net/article/140604.htmapp
1、發送不帶附件的郵件函數
#!/usr/bin/python3 # -*- coding: utf-8 -*- import smtplib from email.header import Header from email.mime.text import MIMEText # 設置郵件頭部顯示 message = MIMEText("郵件測試", 'plain', 'utf-8') # 郵件發送正文(隨意定義) subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') # 郵件顯示主題(隨意定義) message['From'] = '孔紮根' # 郵件顯示發件人(隨意定義) message['To'] = '天心明月' # 郵件顯示收件人(隨意定義) # 配置服務器及帳號信息 sender = 'xx@xxx.cn' # 發件人 receivers = ['xxxx@qq.com'] # 收件人 mail_host = 'smtp.xxxx.cn' # 發送郵件smtp服務器 mail_user = 'xxxx@xxxx.cn' # 登錄郵箱帳號 mail_pass = 'xxxxxzzz!' # 登錄郵箱密碼 try: smtpObj = smtplib.SMTP() # 構造smtp實例 smtpObj.connect(mail_host, 25) # 鏈接smtp服務器 smtpObj.login(mail_user, mail_pass) # 登錄郵箱 smtpObj.sendmail(sender, receivers, message.as_string()) # 發送郵件正文 print("OK") except: print("err")
2、發送帶附件的郵件測試
當不設置文件內容時,能夠發送普通郵件。.net
#!/usr/bin/python3 # -*- coding: utf-8 -*- import smtplib from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # 構建MIMEMultipart對象,並在其中添加郵件內容信息 message = MIMEMultipart() content = MIMEText("郵件測試222222", 'plain', 'utf-8') # 封裝郵件內容 message.attach(content) # 添加到實例 subject = 'Python SMTP 郵件測試' message['Subject'] = Header(subject, 'utf-8') # 主題(隨意定義) message['From'] = '孔紮根' # 發件人(隨意定義) message['To'] = '天心明月' # 收件人(隨意定義) attr = MIMEText(open('./201812.csv', 'rb').read(),'base64', 'utf-8') # 封裝郵件內容 attr['Content-Type'] = 'application/octet-stream' attr['Content-Disposition'] = 'attachment; filename =' + '201812.csv' message.attach(attr) # 添加到實例(文件形式) # 配置服務器及帳號信息 sender = 'xxxx@xxxx.cn' # 發件人 receivers = ['xxxxxx@qq.com'] # 收件人 mail_host = 'smtp.xxxx.cn' # 發送郵件smtp服務器 mail_user = 'xxxx@xxxx.cn' # 登錄郵箱帳號 mail_pass = 'zzzzz' # 登錄郵箱密碼 try: smtpObj = smtplib.SMTP() # 構造smtp實例 smtpObj.connect(mail_host, 25) # 鏈接smtp服務器 smtpObj.login(mail_user, mail_pass) # 登錄郵箱 smtpObj.sendmail(sender, receivers, message.as_string()) # 發送郵件正文 print("OK") except: print("err")
3、模板類htm
有些函數沒有寫出,只是個簡單例子對象
#!/usr/bin/python3 # -*- coding: utf-8 -*- """ 發送郵件: 一、fm=None 發送普通郵件 二、fm 非空 發送帶附件的郵件 """ import smtplib import datetime from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from lion import Base from logCl import log logger = log("郵箱登錄") class SendMyEmail(Base): def __init__(self): Base.__init__(self) self.sec = self.SectionEmail() def loginEmail(self): # 登錄郵箱 try: smtpObj = smtplib.SMTP() # 建立實例 smtpObj.connect(self.sec['host'], 25) # 鏈接smtp smtpObj.login(self.sec['user'], self.sec['password']) # 經過smtp登錄郵箱 logger.info("Login Succ %s" % datetime.datetime.now()) return smtpObj except: logger.error("Login Err %s" % datetime.datetime.now()) return "err" def sendMsg(self, Subject, From, To, msg, fm=None): """ :param Subject: 郵件主題 :param From: 顯示發件人名稱 :param To: 顯示收件人名稱 :param msg: 發送正文內容 :param fm: 發送的附件 """ # 發送郵件 ret = self.loginEmail() if ret != 'err': try: # 設置郵件頭部 message = MIMEMultipart() # 可在此對象中添加 郵件內容和附件內容 message.attach(MIMEText(msg, 'plain', 'utf-8')) # 添加郵件內容 message['Subject'] = Header(Subject, 'utf-8') # 設置郵件主題 message['From'] = From # 設置發件人名稱 message['To'] = To # 設置收件人名稱 # 構造附件 if fm: attr = MIMEText(open(fm, 'rb').read(), 'base64', 'utf-8') # 包裝附件內容 attr['Content-Type'] = 'application/octet-stream' # 設置Content-Type屬性 attr['Content-Disposition'] = 'attachment; filename =' + fm # 設置附件名稱 message.attach(attr) # 添加郵件內容(文件) # 發送郵件 ret.sendmail(self.sec['sender'], self.sec['receiver'].split(","), message.as_string()) logger.info("Send Msg OK %s" % datetime.datetime.now()) except: logger.info("Send Msg Err %s" % datetime.datetime.now()) else: raise Exception("Error user or password, Pleas Check config!!")