优化异常邮件发送

This commit is contained in:
2024-07-12 11:24:55 +08:00
parent 846c83bf93
commit eb690ad02c
4 changed files with 32 additions and 25 deletions

View File

@@ -6,7 +6,7 @@ from email.mime.text import MIMEText
from config.email import ERROR_EMAIL_CONFIG
def send_an_error_email(program_name, error_name, error_detail):
def send_error_email(program_name, error_name, error_detail):
"""
@program_name: 运行的程序名
@error_name: 错误名
@@ -15,16 +15,18 @@ def send_an_error_email(program_name, error_name, error_detail):
"""
# SMTP 服务器配置
smtp_server = ERROR_EMAIL_CONFIG['smtp_server']
sender = ERROR_EMAIL_CONFIG['sender']
authorization_code = ERROR_EMAIL_CONFIG['authorization_code']
receivers = ERROR_EMAIL_CONFIG['receivers']
smtp_server = ERROR_EMAIL_CONFIG["smtp_server"]
port = ERROR_EMAIL_CONFIG["port"]
sender = ERROR_EMAIL_CONFIG["sender"]
authorization_code = ERROR_EMAIL_CONFIG["authorization_code"]
receivers = ERROR_EMAIL_CONFIG["receivers"]
retry_times = ERROR_EMAIL_CONFIG["retry_times"]
# 获取程序出错的时间
error_time = datetime.datetime.strftime(datetime.datetime.today(), "%Y-%m-%d %H:%M:%S:%f")
# 邮件内容
subject = "【程序异常提醒】{name} {date}".format(name=program_name, date=error_time) # 邮件的标题
content = '''<div class="emailcontent" style="width:100%;max-width:720px;text-align:left;margin:0 auto;padding-top:80px;padding-bottom:20px">
subject = f"【程序异常提醒】{program_name} {error_time}" # 邮件的标题
content = f'''<div class="emailcontent" style="width:100%;max-width:720px;text-align:left;margin:0 auto;padding-top:80px;padding-bottom:20px">
<div class="emailtitle">
<h1 style="color:#fff;background:#51a0e3;line-height:70px;font-size:24px;font-weight:400;padding-left:40px;margin:0">程序运行异常通知</h1>
<div class="emailtext" style="background:#fff;padding:20px 32px 20px">
@@ -49,7 +51,7 @@ def send_an_error_email(program_name, error_name, error_detail):
</div>
</div>
</div>
'''.format(program_name=program_name, error_name=error_name, error_detail=error_detail) # 邮件的正文部分
''' # 邮件的正文部分
# 实例化一个文本对象
massage = MIMEText(content, 'html', 'utf-8')
massage['Subject'] = subject # 标题
@@ -57,12 +59,13 @@ def send_an_error_email(program_name, error_name, error_detail):
receivers_str = ','.join(receivers)
massage['To'] = receivers_str # 收件人
log = logging.getLogger()
try:
mail = smtplib.SMTP_SSL(smtp_server, 994) # 连接SMTP服务默认465和944这里用994
mail.login(sender, authorization_code) # 登录到SMTP服务
mail.sendmail(sender, receivers, massage.as_string()) # 发送邮件
mail.quit()
log.info("成功发送了一封邮件到" + receivers_str)
except smtplib.SMTPException:
log.warning("邮件发送失败!")
for i in range(retry_times):
try:
mail = smtplib.SMTP_SSL(smtp_server, port) # 连接SMTP服务
mail.login(sender, authorization_code) # 登录到SMTP服务
mail.sendmail(sender, receivers, massage.as_string()) # 发送邮件
mail.quit()
logging.info("成功发送了一封邮件到" + receivers_str)
except smtplib.SMTPException:
if i == retry_times - 1:
logging.warning("邮件发送失败!")

View File

@@ -1,11 +1,15 @@
# 程序异常短信配置
ERROR_EMAIL_CONFIG = {
# SMTP服务器地址
'smtp_server': 'smtp.163.com',
"smtp_server": "smtp.163.com",
# 连接SMTP的端口
"port": 994,
# 发件人邮箱地址请确保开启了SMTP邮件服务
'sender': 'EchoLiu618@163.com',
"sender": "EchoLiu618@163.com",
# 授权码--用于登录第三方邮件客户端的专用密码,不是邮箱密码
'authorization_code': 'OKPQLIIVLVGRZYVH',
"authorization_code": "OKPQLIIVLVGRZYVH",
# 收件人邮箱地址
'receivers': ['1515783401@qq.com']
"receivers": ["1515783401@qq.com"],
# 尝试次数
"retry_times": 3,
}

View File

@@ -2,7 +2,7 @@ import logging.config
import os
import traceback
from auto_email.error_email import send_an_error_email
from auto_email.error_email import send_error_email
from config.log import LOGGING_CONFIG
from photo_review.photo_review import main
@@ -18,4 +18,4 @@ if __name__ == '__main__':
main()
except Exception as e:
log.error(traceback.format_exc())
send_an_error_email(program_name='照片审核关键信息抽取脚本', error_name=repr(e), error_detail=traceback.format_exc())
send_error_email(program_name='照片审核关键信息抽取脚本', error_name=repr(e), error_detail=traceback.format_exc())

View File

@@ -12,7 +12,7 @@ import paddleclas
from paddlenlp.utils.doc_parser import DocParser
from sqlalchemy import update
from auto_email.error_email import send_an_error_email
from auto_email.error_email import send_error_email
from config.log import LOGGING_CONFIG
from config.mysql import MysqlSession
from config.photo_review import PHHD_BATCH_SIZE, SLEEP_MINUTES
@@ -446,4 +446,4 @@ if __name__ == '__main__':
sleep(SLEEP_MINUTES * 60)
except Exception as e:
logging.error(traceback.format_exc())
send_an_error_email(program_name='照片涂抹脚本', error_name=repr(e), error_detail=traceback.format_exc())
send_error_email(program_name='照片涂抹脚本', error_name=repr(e), error_detail=traceback.format_exc())