7

I am using win32com.client , python 2.7.x and outlook 2013 on windows platform.

I need to post contents of a HTML file to the body of outlook message.
I followed the posts here ,here and here about how to save excel as HTML and paste data within outlook.

However , when I read the file via win32com.client.Dispatch , instead of seeing message, I am seeing HTML code.

Here is the code that converts a processed xlsx file to html format using win32.com.

#Section to convert excel workbook to html 

    myfile = os.getcwd()+ "\\" + outfile
    newfile = os.getcwd()+ "\\" + "emailData.html"
    xl = EnsureDispatch('Excel.Application')
    #xl.Visible = True
    wb3 = xl.Workbooks.Open(myfile)
    wb3WorkSheet = wb3.Worksheets(1)
    wb3WorkSheet.Activate()
    wb3.SaveAs(newfile, constants.xlHtml)
    wb3.Close(True)
    xl.Workbooks.Close()
    xl.Quit()
    del xl

The output of above is newfile which is basically an export of xlsx file saved as html. It is then opened via mail.body handler which should read and display actual contents within outlook.

Here is the code for that.

from win32com.client.gencache import EnsureDispatch
from win32com.client import constants, Dispatch
#Create and open mail message
def Emailer(text, subject, recipient):
    outlook = Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.HtmlBody = text
    #mail.HtmlBody = open(newfile).read()
    mail.body = open(newfile).read()
    attachment1 = os.getcwd()+"//"+outfile
    mail.Attachments.Add(attachment1)
    mail.Display(True)

Emailer(pageTemplate,        
        "test subject",
        "abc@yahoo.com"
        )

So, when I open newfile (html file) using mail.body = open(newfile).read() it pastes html content within a new outlook email body.

When I open newfile (html file) using mail.HtmlBody = open(newfile).read() it's giving following error within outlook email body

ERROR: This page uses frames, but your browser doesn't support them.

Any ideas on this behavior?

I basically want to copy/paste html file (which is an export of xlsx) within outlook email. Not sure if above is correct approach or there are other alternatives.

Is there a way to paste /render HTML frames into outlook email body?

Any pointers is appreciated.

Community
  • 1
  • 1
Anil_M
  • 10,893
  • 6
  • 47
  • 74

1 Answers1

1

You need to set the HTMLBody property, but keep in mind that HTML in Outlook is rendered by Word, not IE, and inline frames are not supported.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • When I set `HTMLBody`, I get `frame` error because HTML data has frames. I cant avoid that. Is there a way to paste /render frame into outlook email body? – Anil_M Apr 29 '16 at 17:24
  • No, as I mentioned before, HTML is handled by Word, not IE. It will display the HTML outside the frames, but the frames will not be loaded. Remember, Word does not go on the network to download the frames. Your HTML must be self contained. – Dmitry Streblechenko Apr 29 '16 at 23:31
  • I get that, but how come a simple copy and paste of the same HTML content with frames from excel to outlook works? Is there a way to achieve that through some code? – Anil_M May 01 '16 at 03:39