I have a python dictionary that I'd like to send an email with in the form of a two column table, where I have a Title and the two column headers, and the key,value pair of the dictionary populated into the rows.
<tr>
<th colspan="2">
<h3><br>title</h3>
</th> </tr>
<th> Column 1 </th>
<th> Column 2 </th>
"Thn dynamic amount of <tr><td>%column1data%</td><td>%column2data%</td></tr>
The column1 and column2 data are the key,value pairs from the associated dictionary.
Is there a way to do this in a simple manner? This is an auotmated email being sent out via a cronjob, once a day after populating the data.
Thank you all. P.S I know nothing about markdown :/
P.S.S I am using Python 2.7
You'll need to create a Sendgrid account with your business domain. Once you access your dashboard, navigate to Email API > Dynamic Templates and then create a new template. Sendgrid allows versioning of templates, so simply, add a version that is going to be an actual template.
Basic Example: (with templating)
#!/usr/bin/env python
from smtplib import SMTP # sending email
from email.mime.text import MIMEText # constructing messages
from jinja2 import Environment # Jinja2 templating
TEMPLATE = """
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
Hello World!.
</body>
</html>
""" # Our HTML Template
# Create a text/html message from a rendered template
msg = MIMEText(
Environment().from_string(TEMPLATE).render(
title='Hello World!'
), "html"
)
subject = "Subject Line"
sender= "root@localhost"
recipient = "root@localhost"
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = recipient
# Send the message via our own local SMTP server.
s = SMTP('localhost')
s.sendmail(sender, [recipient], msg.as_string())
s.quit()
Relevant Documentation:
NB: This assumes you have a valid MTA on your local system.
Note Also: That you may in fact actually want to use a multipart message when composing the email; See Examples
Update: As an aside there are some really nice(er) "email sending" libraries out there that may be of interest to you:
I believe these libraries are along the same lines as requests -- SMTP for Humans
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