SendEmail.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Author renyupeng
  2. # coding=utf-8
  3. # @Time : 2021/11/30 6:02 下午
  4. # @Site :
  5. # @File : SendEmail.py
  6. # @Software: PyCharm
  7. # @contact: renyupeng@c-top.com.cn
  8. # @Tel 1501435553
  9. import smtplib
  10. import time
  11. from email.mime.multipart import MIMEMultipart
  12. from email.mime.text import MIMEText
  13. class SendEmail:
  14. @staticmethod
  15. def mail(content):
  16. try:
  17. # 邮件的内容
  18. my_sender = 'notice@c-top.com.cn' # 发件人邮箱账号
  19. my_pass = 'Hcst@2019' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
  20. recipients = ['liyuyi@c-top.com.cn', 'zhaoxian@c-top.com.cn', 'yumengmeng@c-top.com.cn'] # 收件人邮箱账号
  21. # content = "Please check the attachment. "
  22. msg = MIMEMultipart()
  23. msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z')
  24. msg.attach(MIMEText(content, 'html', 'utf-8'))
  25. # att = MIMEText(open(attachment, 'rb').read(), 'base64', 'utf8')
  26. # att["Content-Type"] = 'application/octet-stream'
  27. # att["Content-Disposition"] = 'attachment; filename="%s"' % attachment
  28. # msg.attach(att)
  29. # [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
  30. msg['From'] = my_sender
  31. # [收件人邮箱昵称、收件人邮箱账号], 昵称随便
  32. msg['To'] = ",".join(recipients)
  33. # 邮件的主题,也就是邮件的标题
  34. msg['Subject'] = "人群分析报表数据异常监控"
  35. # 备注:这里使用的是QQ邮箱的服务器, 加入用腾讯企业邮箱作为发件人的话,请将"smtp.qq.com" 修改为 "smtp.exmail.qq.com"
  36. # 发件人邮箱中的SMTP服务器,qq端口是465
  37. # server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  38. server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
  39. # (发件人邮箱账号、邮箱密码)
  40. server.login(my_sender, my_pass)
  41. # (发件人邮箱账号、收件人邮箱账号、发送邮件)
  42. server.sendmail(my_sender, recipients, msg.as_string())
  43. server.quit() # 关闭连接
  44. print("邮件发送成功")
  45. except Exception as e:
  46. print("邮件发送失败: ", e)