Python Forum
Outlook Emails & HTML Table in Message Body - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Outlook Emails & HTML Table in Message Body (/thread-29427.html)



Outlook Emails & HTML Table in Message Body - JoeDainton123 - Sep-01-2020

Hi all

I was hoping someone could help me with the following python problem.

I am trying to send emails from my outlook account via python. I am able to send plain text messages using the following code which i found online:-

    import win32com.client as client
    outlook = client.Dispatch('Outlook.Application')
    message = outlook.CreateItem(0)
    message.Display()
    message.To = "To_Email"
    message.CC = "CC_Email"
    message.Subject = "Subject"
    message.body = "Body"
    message.Send()
This works great however i attempted to adapt this code to include a name variable and data frame HTML table. The code i use is as follows:-

import pandas
import win32com.client as client

outlook = client.Dispatch('Outlook.Application')
message = outlook.CreateItem(0)
message.Display()
message.To = "To_EMail"
message.CC = "CC_Email1; " + "CC_Email1; " + "CC_Email1"
message.Subject = "Subject"
Name = "Peter"    #name variable goes in the space {0} in the HTML code below
Table = pandas.DataFrame.drop(["Column1", "Column2"], axis=1)  # Table variable goes into the space {1} in the HTML Code Below
    
html = """\
            <html>
              <head> 
              </head>
              <body>
                      Hi {0}
                      <br>
                  <br>
                      TEXT TEXT TEXT 
                  <br> 
                  <br>
                      {1}
                  <br>
                      TEXT TEXT TEXT  TEXT TEXT TEXT  TEXT TEXT TEXT  <br><br>
              TEXT TEXT TEXT 
              <br>
              <br>
              TEXT TEXT TEXT 
              <br
              ><br>
              TEXT TEXT TEXT 
              <br>
              TEXT TEXT TEXT 
              </body>
              
              <br>
    
            </html>
             """.format(Name,Table.to_html())
            
            
message.body = html 
message.Send()
The problem is that i cannot include the table into my email, I have used 2 x spaces {0} for the Name and {1) for the table. But i dont know how to include this table in my email.

The last bit of the code is where the issue is:-

message.body = html
Can anyone help?

Thank you.


RE: Outlook Emails & HTML Table in Message Body - buran - Sep-02-2020

try
message.HTMLbody = html