美文网首页
Python发邮件上报程序错误

Python发邮件上报程序错误

作者: 四爷在此 | 来源:发表于2019-08-18 22:11 被阅读0次

前几天遇到一个python脚本老是出错,也不知道啥时候进程挂掉的。就想加个上报错误的功能,也是第一次遇到。查了一些资料,如下:

通过try catch把 执行过程中的错误catch 到,通过这个reportIssue 函数发送给指定邮箱,上报异常。

from email.mime.text import MIMEText
import smtplib

def reportIssue(issueMsg):
  msg = MIMEText(issueMsg, 'plain', 'utf-8')
  fromAddr = '**@hotmail.com'

  try:
    # 查询自己邮箱服务商的 SMTP server 地址即可
    server = smtplib.SMTP('smtp.live.com', 25) # SMTP协议默认端口是25
    # server.set_debuglevel(1)

    server.ehlo()  # 向 SMTP server发送SMTP 'ehlo' 命令, 紧接着开始 tls 握手
    server.starttls()
    server.login(fromAddr, 'password') # 填上发件邮箱账号,密码
    print('login done')
    server.sendmail(fromAddr, ['dev@aa.com'], msg.as_string())
    server.quit()
    print('sent mail..hahah')
  except Exception as err:
    print(err)

完。。

相关文章

网友评论

      本文标题:Python发邮件上报程序错误

      本文链接:https://www.haomeiwen.com/subject/hnrudctx.html