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

Viewed 240

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 ?

2 Answers

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"

The new line \n character doesn't work on .HtmlBody instead use mail.Body

Example

mail.Body = "This is a return line test \nTest"

HTMLBody Property

Returns or sets a String representing the HTML body of the specified item. The HTMLBody property should be an HTML syntax string

Or you could use <pre> tag set to recognize newlines in HTML

Example

mail.HTMLBody = "<pre>This is a return line test \nTest</pre>"

https://www.w3schools.com/tags/tag_pre.asp

Related