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.
title
Column 1 Column 2
"Thn dynamic amount of
%column1data%%column2data%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
解决方案
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 = """
{{ title }}Hello World!.
""" # 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