Search code examples
pythonemailwin32com

How to add 2 variables in the email body in win32com with Python?


I have this email template:

def email_tamplate(*args):
Format = { 'UNSPECIFIED' : 0, 'PLAIN' : 1, 'HTML' : 2, 'RTF'  : 3}
profile = "Outlook"
#session = win32com.client.Dispatch("Mapi.Session")
outlook = win32com.client.Dispatch("Outlook.Application")
#session.Logon(profile)
mainMsg = outlook.CreateItem(0)
mainMsg.To = "[email protected]"  
mainMsg.Subject = "Automated Crap Daily Update"
mainMsg.BodyFormat = Format['RTF']
mainMsg.HTMLBody = body2


mainMsg.Send()  #this line actually sends the email

And would like to send an email that has 2 tables in the body. So I have 2 bodies: Here's one:

 eod = []
body2 = ['<html><body><table border="1" style="width:300px"><tr><td>Title Level</td></tr><tr><td>Source</td><td>Count</td></tr>']
header = [['Title Level']]

for row in cur:
    eod.append(row)


count=0
count2=0
for item in eod:
    body2[0]=body2[0]+"<tr><td>"+str(eod[count2][count])+"</td><td>"+str(eod[count2][count+1])+"</td></tr>"
    count2=count2+1

body2[0]=body2[0]+"</table></body></html>"
body2=body2[0]
globals().update(locals())

And here's the other:

eod = []
body = ['<html><body><table border="1" style="width:300px"><tr><td>Previous Day</td></tr><tr><td>Decision_Status</td><td>Count</td></tr>']
header = [['Prev Day']]

for row in cur:
    eod.append(row)



count=0
count2=0
for item in eod:
    body[0]=body[0]+"<tr><td>"+str(eod[count2][count])+"</td><td>"+str(eod[count2][count+1])+"</td></tr>"
    count2=count2+1

body[0]=body[0]+"</table></body></html>"
body=body[0]
globals().update(locals())

Both are created with data from different queries. So I would like to be able to send in the body of the email variables body and boody2 Any ideas of how to accomplish this?

Thank you


Solution

  • I just resolved the issue. It happens that I only needed to concatenate body + body2.

    As simple as that. But thank you!