Search code examples
pythonhtmloutlook

Python create a new mail. Return Line (\n) in body not working


I'm working on a python script which should create a new mail with a specific text body.

Fact is that \n does not work as excepted.

My code is below :

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.HtmlBody = "This is a return line test \nTest"

The body displayed is :

This is a return line test Test

And not :

This is a return line test 
Test

What Am I missing ?


Solution

  • Since you are using HtmlBody , instead of \n, you want to use the HTML style tags for your new-lines. These are called line-breaks, and they look like this: <br>. So you want to replace \n with <br>:

    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.HtmlBody = "This is a return line test <br>Test"