想着能用程序自動發郵件就行了,想到就動手寫了一個自動發郵件的腳本。html
一:前提準備(我使用的是qq郵箱)服務器
一、開通qq郵箱的smtp服務app
二、上代碼ui
# -*- coding: utf-8 -*- # @Time : 18-10-14 上午11:00 # @Author : Felix Wang from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.utils import parseaddr, formataddr from email.header import Header import smtplib from hashlib import md5 # 自動發郵件 class autoSendEmail: def __init__(self, sender, password, title, from_who, recever, smtp_server="smtp.qq.com", port=465): """ :param sender: 郵件發送者 :param password: 密碼 :param title: 郵件發送主題 :param from_who: 郵件來自誰 :param recever: 郵件接收者,能夠是多個 :param smtp_server: 郵件服務器,默認qq郵箱服務器 :param port: 服務器端口qq郵箱默認端口爲465 """ self.smtp_server = smtp_server # 使用qq轉發須要用到,能夠在QQ郵箱設置中查看並開通此轉發功能 self.smtp_port = port # smtp默認的端口是465 # 接受者能夠是多個,放在列表中 self.recever = recever self.sender = sender self.password = password # 該密碼是配置qq郵箱的SMTP功能的受權碼 self.msg = MIMEMultipart() self.msg['Subject'] = title # 郵件標題 self.msg['From'] = self._format_addr(u'{} <{}>'.format(from_who, self.sender)) # 添加文字信息 def addTextMsg(self, text): text_plain = MIMEText(text, 'plain', 'utf-8') self.msg.attach(text_plain) # 添加圖片 def addImageMsg(self, imgPath): extend = imgPath.split('.')[-1] with open(imgPath, 'rb')as f: sendimagefile = f.read() filename = md5(sendimagefile).hexdigest() + '.' + extend image = MIMEImage(sendimagefile) image.add_header('Content-ID', '<image1>') image["Content-Disposition"] = u'attachment; filename={}'.format(filename) self.msg.attach(image) # 添加附件 def addFile(self, filePath): extend = filePath.split('.')[-1] with open(filePath, 'rb')as f: sendfile = f.read() filename = md5(sendfile).hexdigest() + '.' + extend # 構造附件 text_att = MIMEText(sendfile, 'base64', 'utf-8') text_att["Content-Type"] = 'application/octet-stream' text_att["Content-Disposition"] = u'attachment; filename="{}"'.format(filename) self.msg.attach(text_att) # 添加html格式 def addHtml(self, html): # 構造html # 發送正文中的圖片:因爲包含未被許可的信息,網易郵箱定義爲垃圾郵件,報554 DT:SPM :<p><img src="cid:image1"></p> text_html = MIMEText(html, 'html', 'utf-8') self.msg.attach(text_html) # 格式化郵件地址 def _format_addr(self, s): name, address = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), address)) # 發送郵件 def sendEmail(self): server = smtplib.SMTP_SSL(self.smtp_server, self.smtp_port) # 連接服務器 server.set_debuglevel(1) # 打印出和SMTP服務器交互的信息 server.login(self.sender, self.password) # 登陸 server.sendmail(self.sender, self.recever, self.msg.as_string()) # 發送郵件 server.quit() # 退出 print('郵件發送成功') if __name__ == '__main__': smtp_server = "smtp.qq.com" # smtp服務地址 port = 465 # 端口號 recever = ['xxx@qq.com'] # 接收人列表能夠是多個 sender = "xxx@qq.com" # 發送人郵箱 password = "" # 若是是qq郵箱的話該密碼是配置qq郵箱的SMTP功能的受權碼 title = '你好' from_who = 'felix' # 發送人姓名 # 實例化對象 autoEmail = autoSendEmail(sender=sender, recever=recever, password=password, title=title, from_who=from_who, smtp_server=smtp_server, port=port) # 單純發送文字 autoEmail.addTextMsg('你好啊') # 以附件的形式發送圖片,這種方式能夠直接用addfile代替 autoEmail.addImageMsg('felix.jpeg') # 發送附件 autoEmail.addFile('你好.py') autoEmail.addFile('felix.jpeg') html = """ <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://www.baidu.com">link</a> you wanted.<br> </p> <img src="http://img.zcool.cn/community/01f09e577b85450000012e7e182cf0.jpg@1280w_1l_2o_100sh.jpg"></img> </body> </html> """ # 以html的形式發送文字,推薦這個,由於能夠添加圖片等 autoEmail.addHtml(html) # 發送郵件 try: autoEmail.sendEmail() except Exception as e: print(e) print('郵件發送失敗')