0

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

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ansh1
  • 65
  • 4
  • @enzo Do you mean send_error_email(exp_message='
    '.join(format_exc().splitlines()))
    – ansh1 Oct 20 '21 at 08:12
  • I'm not sure that there is a question being asked here at all; but if there is, it seems to be about *what the output HTML data should be*. – Karl Knechtel Aug 06 '22 at 01:29

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
  • 141
  • 1
  • 2
  • 12
  • 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
  • 684
  • 1
  • 10
  • 22
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,861
  • 3
  • 15
  • 38