Reputation: 1853
I am trying to send an email using python. I am trying to format my email in HTML format. Below is the code:-
sender = '[email protected]'
receivers = ['[email protected]']
message = message = """From: team <[email protected]>
To: To Person <[email protected]>
MIME-Version: 1.0
Content-type: text/html
Subject: Daily Report
<html>
<table>
Report
<% for i in rows: %>
<tr>
<td> <%= rows[0][1] %>
</tr>
<% end %>
</table>
<b>This is HTML message.</b>
<h1>This is headline.</h1>
</html>
"""
smtpObj = smtplib.SMTP('SMTP mailer')
smtpObj.sendmail(sender, receivers, message)
I am fetching rows from Database based on some condition and storing it in "rows" variable. I am trying to loop the HTML table creation based on the number of rows returned.
Whenever i send an email it does not identify the for loop that i have written. It does identify the "This is HTML message" in Bold form and "This is headline" in H1 format
What am i doing wrong?
Upvotes: 0
Views: 859
Reputation: 8932
You want to use a template engine for creating the HTML. For Python the message string you are using is just a string, nothing else. The `<% for i in rows: %>' is this very text, Python does no processing whatsoever with it.
Your mail message is not correctly formatted according to RFC2822:
For sending an HTML formatted message with Python, I would suggest that you use the features from email.mime
, c.f. How to send an email with style in Python3?
Upvotes: 2