mail_helper.py是邮件操作包,用来发送邮件的。

 1 #!/usr/bin/evn python
 2 # coding=utf-8
 3 
 4 import smtplib
 5 from email.mime.text import MIMEText
 6 from traceback import format_exc
 7 from config import const
 8 
 9 # 初始化邮件参数
10 smtp = const.SMTP
11 port = const.PORT
12 user = const.EMAIL_USER
13 passwd = const.EMAIL_PWD
14 email_list = const.EMAIL_LIST
15 err_title = const.EMAIL_ERR_TITLE
16 
17 
18 def send_mail(subject, context, to_list):
19     '''
20     发送邮件
21     接收参数:
22     subject 邮件主题
23     context 邮件内容
24     to_list 接收者邮件列表,每个邮件地址用","分隔
25     '''
26     if not subject or not context or not to_list:
27         return '邮件发送失败,邮件主题、内容与收件人邮件都是必填项'
28 
29     # 初始始化邮件相关参数
30     email = MIMEText(context, 'html', 'utf-8')
31     email['To'] = to_list
32     email['Subject'] = subject
33     email['From'] = user
34 
35     # QQ邮箱改为ssl方式发送了
36     # s = smtplib.SMTP(smtp)
37     s = smtplib.SMTP_SSL(smtp)
38     try:
39         s.login(user, passwd)
40         s.sendmail(user, email_list, email.as_string())
41         s.close()
42         return None
43     except Exception as e:
44         s.close()
45         stacktrace = format_exc()
46         return '邮件发送失败,出现异常:' + str(e.args) + stacktrace + '\n'
47 
48 
49 def send_error_mail(context):
50     '''
51     发送邮件
52     接收参数:
53     context 邮件内容
54     '''
55     if not context:
56         return '邮件内容是必填项'
57 
58     send_mail(err_title, context, email_list)
View Code

相关文章:

  • 2021-11-30
  • 2021-09-21
  • 2021-06-10
  • 2021-11-17
  • 2021-09-11
  • 2021-08-03
  • 2021-12-31
猜你喜欢
  • 2021-05-21
  • 2021-12-29
  • 2022-03-02
  • 2022-01-07
  • 2021-09-07
  • 2021-06-16
相关资源
相似解决方案