Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook 2013 display HTML code instead of actual Data

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",
        "[email protected]"
        )

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.

like image 563
Anil_M Avatar asked Apr 26 '16 21:04

Anil_M


People also ask

Why is my email showing HTML code?

Go to your email notification settings for this email and make sure the box to "Send Emails in Plain Text" is not checked. Check for a plugin conflict. We've seen several plugins that force all emails to be sent as either plain text or HTML cause this issue. Please deactivate your plugins and resend the email.

How do I switch to HTML in Outlook?

Change the message format for all messages you sendOn the File tab, choose Options > Mail. Under Compose messages, in the Compose messages in this format list, click HTML, Rich Text, or Plain Text.

How do I change the reply from plain text to HTML in Outlook?

Changing email format to HTML in Outlook First, there is always the option to switch the email format manually. When you reply to or forward a message, you can go to Format Text on the Outlook's ribbon and click HTML.

Why email received in plain text instead of HTML?

There can be various reasons for this behaviour. Most commonly, this issue occurs if the Remote Domain configuration settings specified on the Microsoft Exchange server are incorrectly configured.


1 Answers

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.

like image 67
Dmitry Streblechenko Avatar answered Sep 17 '22 14:09

Dmitry Streblechenko