html in python to send e mails

I am new to python and I am trying to send e-mails from python. I would like to add some python code inside my html, but it doesn’t seem to work. In fact I used the {%} inside the html code but i have an error telling me that is an invalid syntax. Below an extract of my code :

import win32com.client as client
from datetime import date

outlook = client.Dispatch('Outlook.Application')
message = outlook.CreateItem(0)
message.To = 'XXXXXX'

message.Subject = 'Data update '+ str(
message.HTMLBody = f"""<div> Hi, <br> Here is the report of the data update : </div>

<br> <br> <b> <u> Bases : </u> </b>
<br> The update of the following databases failed : 
<br> {%for value in range len(final_result):
            final_result['Tables'][value] +" : "+ final_result['date_donnees'][value]%}
<br> <br> Les reportings ayant échoué sont : {failed_reports} """

message.Save() # save to drafts folder
message.Send() # send to outbox

The dataframe “final_result” are of course declared a little higher in the code.

Does anyone know how to deal with this ?

Thank you very much !


You need to create your string outside of the HTML, and just include it as a string.

res = ""
for value in range(len(final_result)):
    res += final_result['Tables'][value] + " : " + final_result['date_donnees'][value]

message.HTMLBody = f"""...
<br> {res}

I’m not sure len(final_result) is the right answer, but see if this gets you closer.