python email 工具类

 1 # coding=utf-8
 2 import smtplib
 3 from email.mime.text import MIMEText
 4 
 5 
 6 class ErrorEmail():
 7     def __init__(self, msg_from, pass_word, msg_to):
 8         '''
 9         发送邮件实体类
10         :param msg_from: # 发送方邮箱
11         :param pass_word: # 填入发送方邮箱的授权码
12         :param msg_to: # 收件人邮箱
13         '''
14         self.msg_from = msg_from
15         self.pass_word = pass_word
16         self.msg_to = msg_to
17 
18     def theme_content(self, theme, content):
19         '''
20         构建邮件主题和内容
21         :param theme:  主题
22         :param content:  内容
23         :return:
24         '''
25         msg = MIMEText(content)
26         msg['Subject'] = theme
27         msg['From'] = self.msg_from
# join 作用群发邮件
28 msg['To'] = ''.join(self.msg_to) 29 return msg 30 31 def send_message(self, host, port, msg): 32 ''' 33 发送邮件方法 34 :param host: 第三方邮件host 35 :param port:端口号 36 :param msg:MIMETexe 对象 37 :return: 38 ''' 39 try: 40 sm = smtplib.SMTP_SSL(host, port) 41 sm.login(self.msg_from, self.pass_word) 42 sm.sendmail(self.msg_from, self.msg_to, msg.as_string()) 43 except Exception as e: 44 print(e) 45 finally: 46 sm.quit() 47 48 49 if __name__ == '__main__': 50 ee = ErrorEmail('发送邮箱地址', '授权码', ['接收方邮件地址']) 51 msg = ee.theme_content('主题', '内容')
# smtp.exmail.qq.com 是腾讯企业邮箱的host 和端口号
52 ee.send_message("smtp.exmail.qq.com", 465, msg)

 

posted @ 2020-06-03 09:51  Waking_up  阅读(248)  评论(0编辑  收藏  举报