如何获取异常消息,然后发送包含该异常错误的 HTML 电子邮件

How to fetch an exception message and then send an HTML email containing that exception error

如何将下面 test_file1 的异常传递给 test_file2,以便它可以发送包含该异常信息的电子邮件?

test_file1.py

    import sys
    try:
        print(5/0)
    except Exception as e:
        print(e)
        print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))

输出:

integer division or modulo by zero
Error on line 15

test_file2.py

import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    me = "my@email.com"
    you = "your@email.com"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Link"
    msg['From'] = me
    msg['To'] = you
    
    text = "Exception mail"
    html = """\
    <html>
      <body>
    
        <b> Exception Details: </b>print('Exception: {}'.format(e))
        <b>
            Error on line {}.format(sys.exc_info()[-1].tb_lineno)
        </b>
        
      </body>
    </html>
    """
    
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    
    msg.attach(part1)
    msg.attach(part2)
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    mail.ehlo()
    
    mail.starttls()
    
    mail.login('userName', 'password')
    mail.sendmail(me, you, msg.as_string())
    mail.quit()

所以基本上来自 file1.py 的异常消息应该传递给 file2.py 以发送带有该错误消息的邮件:

>> Exception Details: integer division or modulo by zero
>> Found Exception!
>> Error on line 15

您可能想在 test_file2.py 中使用一个函数,然后只需导入并在 test_file1.py 中使用它。请注意,我通过将 f 添加到您的 html 字符串来使用格式化字符串,因此我可以在字符串中使用变量。

test_file2.py:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText


def send_error_email(exception, message):
    me = "my@email.com"
    you = "your@email.com"
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Link"
    msg['From'] = me
    msg['To'] = you

    text = "Exception mail"
    html = f"""
        <html>
          <body>
    
            <b> Exception Details: </b> Exception: {exception}
            <b>
                {message}
            </b>
    
          </body>
        </html>
        """

    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    msg.attach(part1)
    msg.attach(part2)
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    mail.ehlo()

    mail.starttls()

    mail.login('userName', 'password')
    mail.sendmail(me, you, msg.as_string())
    mail.quit()

test_file1.py:

import sys
from test_file2 import send_error_email

try:
    print(5 / 0)
except Exception as e:
    print(e)
    print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
    send_error_email(exception=e, message='Error on line {}'.format(sys.exc_info()[-1].tb_lineno))

编辑:如果你想要更多的细节,你可以使用traceback包:

import traceback
from test_file2 import send_error_email

try:
    print(5 / 0)
except Exception as e:
    print(e)
    message=f'Error on line {traceback.format_exc()}'
    print(message)
    send_error_email(exception=e, message=message)

编辑#2: 要使用较早的 .format() 作为字符串,您可以传入参数

html = """
        <html>
          <body>

            <b> Exception Details: </b> Exception: {exception}
            <b>
                {message}
            </b>

          </body>
        </html>
        """.format(exception=exception, message=message)