|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +''' |
| 4 | + @Author: Yong Gao, |
| 5 | + Jamie Zhu |
| 6 | + @Date: 2018-11-17 |
| 7 | +''' |
| 8 | + |
| 9 | + |
| 10 | +import os |
| 11 | +import smtplib |
| 12 | +from email.mime.text import MIMEText |
| 13 | +from email.mime.multipart import MIMEMultipart |
| 14 | +from email.mime.image import MIMEImage |
| 15 | +from email.header import Header |
| 16 | +from email.utils import formataddr |
| 17 | + |
| 18 | +class EmailAPI(): |
| 19 | + # 目前仅实现了 163 和 qq 邮箱两种; 其他邮箱服务器需另行查找设置; |
| 20 | + # QQ邮箱需要设置授权码, 参考https://service.mail.qq.com/cgi-bin/help?subtype=1&id=28&no=1001256 |
| 21 | + def __init__(self, username, password): |
| 22 | + self.mail_user = username |
| 23 | + self.mail_pass = password |
| 24 | + email_type_dict = { |
| 25 | + '163.com': 'no_ssl', |
| 26 | + 'qq.com': 'no_ssl', |
| 27 | + 'logpai.com': 'no_ssl' |
| 28 | + } |
| 29 | + mail_host_dict = { |
| 30 | + 'qq.com': "smtp.qq.com", |
| 31 | + '163.com': "smtp.163.com", |
| 32 | + 'logpai.com': "smtp.qiye.aliyun.com" |
| 33 | + } |
| 34 | + email_type = username.split('@')[1] |
| 35 | + self.mail_host = mail_host_dict[ email_type ] |
| 36 | + if email_type in email_type_dict: |
| 37 | + self.mail_type = email_type_dict[ email_type ] |
| 38 | + else: |
| 39 | + print( '暂时不支持的邮箱类型' ) |
| 40 | + self.mail_type = 'ssl' |
| 41 | + self.login() |
| 42 | + |
| 43 | + def set_reciv_list(self, receivers_list): |
| 44 | + # 发件人收件人信息格式化 ,可防空 |
| 45 | + # 固定用法不必纠结,我使用lambda表达式进行简单封装方便调用 |
| 46 | + lam_format_addr = lambda name, addr: formataddr((Header(name, 'utf-8').encode(), addr)) |
| 47 | + |
| 48 | + self.msg = MIMEMultipart() |
| 49 | + self.msg['From'] = lam_format_addr("LogPAI Team", self.mail_user) # 腾讯邮箱可略 |
| 50 | + self.msg['To'] = ";".join(receivers_list) #lam_format_addr('', receivers_list[0]) # 腾讯邮箱可略 |
| 51 | + |
| 52 | + def set_email_msg(self, to_list, msg_title, msg_content, content_type='str' ): |
| 53 | + ''' |
| 54 | + content_type = str/html/html_file |
| 55 | + ''' |
| 56 | + self.to_list = to_list |
| 57 | + self.set_reciv_list(to_list) |
| 58 | + self.msg['Subject'] = Header( msg_title, 'utf-8').encode() # 腾讯邮箱略过会导致邮件被屏蔽 |
| 59 | + if content_type == 'str': |
| 60 | + content_main = MIMEText(msg_content, 'plain', 'utf-8') |
| 61 | + elif content_type == 'html': |
| 62 | + content_main = MIMEText(msg_content, 'html', 'utf-8') |
| 63 | + elif content_type == 'html_file': |
| 64 | + with open(msg_content,'r') as f: |
| 65 | + content = f.read() |
| 66 | + #设置html格式参数 |
| 67 | + content_main = MIMEText(content, 'html', 'utf-8') |
| 68 | + self.msg.attach(content_main) |
| 69 | + |
| 70 | + def add_attach_pic(self, pic_path_name): |
| 71 | + file_path, file_name = os.path.split(pic_path_name) |
| 72 | + #添加照片附件 |
| 73 | + with open(pic_path_name, 'rb')as fp: |
| 74 | + picture = MIMEImage(fp.read()) |
| 75 | + #与txt文件设置相似 |
| 76 | + picture['Content-Type'] = 'application/octet-stream' |
| 77 | + picture['Content-Disposition'] = 'attachment;filename="{}"'.format(file_name) |
| 78 | + #将内容附加到邮件主体中 |
| 79 | + self.msg.attach(picture) |
| 80 | + |
| 81 | + def add_attach_file(self, file_path_name): |
| 82 | + file_path, file_name = os.path.split(file_path_name) |
| 83 | + #添加一个txt文本附件 |
| 84 | + with open(file_path_name,'r')as h: |
| 85 | + attach_file = h.read() |
| 86 | + #设置txt参数 |
| 87 | + attach_file = MIMEText(attach_file, 'plain', 'utf-8') |
| 88 | + #附件设置内容类型,方便起见,设置为二进制流 |
| 89 | + attach_file['Content-Type'] = 'application/octet-stream' |
| 90 | + #设置附件头,添加文件名 |
| 91 | + attach_file['Content-Disposition'] = 'attachment;filename="{}"'.format(file_name) |
| 92 | + self.msg.attach(attach_file) |
| 93 | + |
| 94 | + def send_email(self): |
| 95 | + #登录并发送 |
| 96 | + |
| 97 | + try: |
| 98 | + self.smtpObj.sendmail( self.mail_user, self.to_list, self.msg.as_string() ) |
| 99 | + print('Send success: ' + str(self.to_list)) |
| 100 | + except Exception as e: |
| 101 | + print('Error: ', e) |
| 102 | + |
| 103 | + |
| 104 | + def login(self): |
| 105 | + smtpObj = None |
| 106 | + try: |
| 107 | + if self.mail_type =='no_ssl': |
| 108 | + smtpObj = smtplib.SMTP() |
| 109 | + smtpObj.connect(self.mail_host, 25) |
| 110 | + elif self.mail_type == 'ssl': |
| 111 | + smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) |
| 112 | + |
| 113 | + smtpObj.login(self.mail_user, self.mail_pass) |
| 114 | + self.smtpObj = smtpObj |
| 115 | + print('Login success.') |
| 116 | + except: |
| 117 | + print('Login failed.') |
| 118 | + raise |
| 119 | + |
| 120 | + def close(self): |
| 121 | + self.smtpObj.quit() |
| 122 | + |
| 123 | + |
| 124 | +if __name__=='__main__': |
| 125 | + # Example |
| 126 | + email = EmailAPI( username='[email protected]', password='*****') |
| 127 | + to_list = [ '[email protected]' ] # Set mailto list |
| 128 | + msg_subject = "Notice: This is a test." # Set email subject |
| 129 | + msg_content = "This is a test.\n\nLogPAI Team" # Set email content |
| 130 | + email.set_email_msg(to_list, msg_subject, msg_content) |
| 131 | + email.send_email() |
| 132 | + email.close() |
| 133 | + |
| 134 | + |
| 135 | + |
0 commit comments