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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With