diff --git a/auto_email/error_email.py b/auto_email/error_email.py index 9a6a5b1..91af21e 100644 --- a/auto_email/error_email.py +++ b/auto_email/error_email.py @@ -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 = '''
+ subject = f"【程序异常提醒】{program_name} {error_time}" # 邮件的标题 + content = f'''

程序运行异常通知

@@ -49,7 +51,7 @@ def send_an_error_email(program_name, error_name, error_detail):
- '''.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("邮件发送失败!") diff --git a/config/email.py b/config/email.py index ec7e39c..c7cd056 100644 --- a/config/email.py +++ b/config/email.py @@ -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, } diff --git a/main.py b/main.py index 95ef10c..bfd5df0 100644 --- a/main.py +++ b/main.py @@ -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()) diff --git a/photo_mask.py b/photo_mask.py index 7260a0f..f73ae4d 100644 --- a/photo_mask.py +++ b/photo_mask.py @@ -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())