Python Forum
Outlook Emails & HTML Table in Message Body
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Outlook Emails & HTML Table in Message Body
#1
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.
Reply
#2
try
message.HTMLbody = html 
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Going through HTML table with selenium emont 3 802 Sep-30-2023, 02:13 AM
Last Post: emont
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 742 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Search Outlook Inbox for set of values cubangt 1 1,063 Jun-28-2023, 09:29 PM
Last Post: cubangt
  Save image from outlook email cubangt 1 694 Jun-07-2023, 06:52 PM
Last Post: cubangt
  Tkinterweb (Browser Module) Appending/Adding Additional HTML to a HTML Table Row AaronCatolico1 0 923 Dec-25-2022, 06:28 PM
Last Post: AaronCatolico1
  Read All Emails from Outlook and add the word counts to a DataFrame sanaman_2000 0 1,854 Sep-15-2022, 07:32 AM
Last Post: sanaman_2000
  Sending Emails on Autopilot Gyga_Hawk 3 1,683 Mar-15-2022, 08:20 AM
Last Post: Larz60+
  Mark outlook emails as read using Python! shane88 2 6,542 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Trying out the parsing/reading of emails from my outlook cubangt 0 6,150 Jan-12-2022, 08:59 PM
Last Post: cubangt
  reading html and edit chekcbox to html jacklee26 5 3,071 Jul-01-2021, 10:31 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020