1

I am using html email and .format() for string to pass in the arguments

Below are my python code :

import sys

try:
    print(5 / 0)
except Exception as e:
    send_error_email(exp_message=format_exc())

and then fetching the function send_error_email and pass to MIMEMultipart mail script

 html = """
            <html>
              <body>
    
                <b> Exception Details: </b> {exp_message}
    
              </body>
            </html>
            """.format(exp_message=exp_message)

Getting Output in one line in mail :

Exception Data : Traceback (most recent call last): File "C:\Build\test\workfile\python-practice\exception_test.py", line 54, in get_details print(100/0) ZeroDivisionError: division by zero

Expected Output should be every message in new line:

Exception Data : Traceback (most recent call last):
File "C:\Build\test\workfile\python-practice\exception_test.py", line 54,
in get_details print(100/0)
ZeroDivisionError: integer division or modulo by zero

ansh1
  • 67
  • 4

3 Answers3

0

In the HTML, you can use a line break <br> to put the exception on another line. Also, you can change up your formatting just a bit. Since there's only one argument in the formatter, you can just use positional arguments and not keyword arguments.

 html = """
            <html>
              <body>
                <b> Exception Data: </b>
                <br>
                {}
              </body>
            </html>
            """.format(exp_message)
DevGuyAhnaf
  • 133
  • 2
  • 11
  • I tried still all trackeback messege is printing in one line only , I need to make every data to new line – ansh1 Oct 20 '21 at 07:44
0

For Better and Pretty Printing of Exceptions, Refer the Python's Traceback Library
Specifically, check the traceback.format_* methods (eg. format_exception())

HIMANSHU PANDEY
  • 541
  • 8
  • 21
0

Just replace the Python's newline with the HTML's line break.

html_exp_message = exp_message.replace('\n', '<br>')

html = """
            <html>
              <body>
                <b> Exception Details: </b> {}
              </body>
            </html>
            """.format(html_exp_message)
enzo
  • 9,121
  • 2
  • 12
  • 36