# -*- coding:utf-8 -*- # __author__ = 'justing' import os import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication SENDER = "xxx" PASSWD = "xxx" SMTPSERVER = "xxx" class Mail(object): """ SMTP是發送郵件的協議,Python內置對SMTP的支持,能夠發送純文本郵件、HTML郵件以及帶附件的郵件。 Python對SMTP支持有smtplib和email兩個模塊,email負責構造郵件,smtplib負責發送郵件。 注意:郵箱必須開啓SMTP才能夠經過該腳本發郵件 MIMEBase |-- MIMENonMultipart |-- MIMEApplication |-- MIMEAudio |-- MIMEImage |-- MIMEMessage |-- MIMEText |-- MIMEMultipart 通常來講,不會用到MIMEBase,而是直接使用它的繼承類。MIMEMultipart有attach方法,而MIMENonMultipart沒有,只能被attach。 MIME有不少種類型,這個略麻煩,若是附件是圖片格式,我要用MIMEImage,若是是音頻,要用MIMEAudio,若是是word、excel,我都不知道該用哪一種MIME類型了,得上google去查。 最懶的方法就是,無論什麼類型的附件,都用MIMEApplication,MIMEApplication默認子類型是application/octet-stream。 """ def __init__(self, receivers, subject, content, content_type=None, attachment=None, sender=SENDER, passwd=PASSWD, smtp_server=SMTPSERVER): self.sender = sender self.passwd = passwd self.smtp_server = smtp_server #receivers type list self.receivers = receivers self.subject = subject self.content = content self.content_type = content_type #attachement type is list or str self.attachment = attachment def attach(self, path): filename = os.path.basename(path) with open(path, 'rb') as f: info = f.read() attach_part = MIMEApplication(info) attach_part.add_header('Content-Disposition', 'attachment', filename=filename) self.msg.attach(attach_part) def handle_attachment(self): # 支持多個附件 if isinstance(self.attachment, list): for path in self.attachment: self.attach(path) if isinstance(self.attachment, str): self.attach(path) def handle(self): if not self.content_type or self.content_type == "text": text = MIMEText(self.content, 'plain', 'utf-8') elif self.content_type == "html": text = MIMEText(self.content, _subtype='html', _charset='utf-8') else: raise "type only support utf and text" self.msg.attach(text) if self.attachment: self.handle_attachment() def send(self): # 如名字所示: Multipart就是多個部分 self.msg = MIMEMultipart() self.msg['From'] = self.sender #msg['To']接收的是字符串而不是list,若是有多個郵件地址,用,分隔便可。 self.msg['To'] = ','.join(self.receivers) self.msg['Subject'] = self.subject self.handle() try: server = smtplib.SMTP(self.smtp_server) #set_debuglevel(1)就能夠打印出和SMTP服務器交互的全部信息。 #server.set_debuglevel(1) server.ehlo() #加密:調用starttls()方法,就建立了安全鏈接 server.starttls() server.login(self.sender, self.passwd) #self.receivers type list server.sendmail(self.sender, self.receivers, self.msg.as_string()) server.quit() except Exception, e: print "fail to send mail:{}".format(e) receivers = ['xxx@lenovo.com', 'xxx@qq.com'] subject = "This is a TEST" content = "hello kitty" content_type = "html" path = r"D:\workspace\script" attachment = [] # for parent_dir, child_dirs, filenames in os.walk(path): # print "parent_dir>>", parent_dir # print "filenames", filenames # for filename in filenames: # if filename.split(".")[-1] in ["jpg", "xlsx", "mp3", "png"]: # attachment.append(os.path.join(parent_dir, filename)) mail = Mail(receivers, subject, content, content_type, attachment) mail.send()