python email exception error

Solutions on MaxInterview for python email exception error by the best coders in the world

showing results for - "python email exception error"
Euen
12 Jun 2019
1#This gives feedback on errors within a script which then emails output
2
3import win32com.client
4import os
5import sys
6
7try:
8  #Some code
9  
10except Exception as e:
11    exc_type, exc_obj, exc_tb = sys.exc_info()
12    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
13    print((exc_type, fname, exc_tb.tb_lineno, str(e)))
14    error_line  = str(exc_type) + '  ' + str(fname) + '  Line: ' + str(exc_tb.tb_lineno) + '  ' + str(e)
15    
16    olMailItem = 0x0
17    obj = win32com.client.GetActiveObject("Outlook.Application")
18    newMail = obj.CreateItem(olMailItem)
19    newMail.Subject = "There's a snake in my boot!"
20    newMail.HTMLBody = """Morning
21                      <br>
22                      <br>Your script failed!
23                      <br>
24                      <br>{}#Script name
25                      <br>{}#Line number where failed
26                      <br>{}#Error goes here
27                      <br>
28                      <br>Kind regards""".format('Script: '+str(fname),'Line: '+str(exc_tb.tb_lineno),'Error: '+ str(e))
29    newMail.To = "Help@Fixthis.com"
30    newMail.Send()