SMTPLIB
A python base package to send emails.
Within this post, we will cover.
- How to use prettytable library to create tabular data in console.
- How to convert prettytable tabular data output in HTML format
- How we can send HTML email using Python.
- How can we integrate mime with email.
- Code snippet to send HTML email along with Texr
- How to send email as MultiPart message content ( Text + HTML)
We will start with the use case:
- I have a html table and and a Link that must be within the same email body and not as an attachment.
- So for now, i am using PreetyTable to provide output of my table in HTML format.
Intended Table.
Firstname | Lastname | Age |
---|---|---|
Jill | Smith | 50 |
Eve | Jackson | 94 |
John | Doe | 80 |
How to create Tabular data in CLI using prettytable.
mytable.pyfrom prettytable import PrettyTable
tabular_fields = ["Firstname", "Lastname", "Age"]
tabular_table = PrettyTable()
tabular_table.field_names = tabular_fields
tabular_table.add_row(["Jill","Smith", 50])
tabular_table.add_row(["Eve","Jackson", 94])
tabular_table.add_row(["John", "Doe", 80])
>>> print(tabular_table)
+-----------+----------+-----+
| Firstname | Lastname | Age |
+-----------+----------+-----+
| Jill | Smith | 50 |
| Eve | Jackson | 94 |
| John | Doe | 80 |
+-----------+----------+-----+
To print the data as an HTML output use the below code snippet
mytable.pyfrom prettytable import PrettyTable
tabular_fields = ["Firstname", "Lastname", "Age"]
tabular_table = PrettyTable()
tabular_table.field_names = tabular_fields
tabular_table.add_row(["Jill","Smith", 50])
tabular_table.add_row(["Eve","Jackson", 94])
tabular_table.add_row(["John", "Doe", 80])
>>> print(tabular_table.get_html_string())
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
Now as we already know how to create html output for a specific table using prettytable, we will use the same html output within our email body.
mytable.py
from prettytable import PrettyTable tabular_fields = ["Firstname", "Lastname", "Age"] tabular_table = PrettyTable() tabular_table.field_names = tabular_fields tabular_table.add_row(["Jill","Smith", 50]) tabular_table.add_row(["Eve","Jackson", 94]) tabular_table.add_row(["John", "Doe", 80]) >>> print(tabular_table.get_html_string())
<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
- Here is the code.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def trigger_email():
my_message = tabular_table.get_html_string()
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<p>Customer Inventory!<br>
Here are the details?<br>
Link to register. <a href="http://www.python.org">link</a> you wanted.<br>
%s
</p>
</body>
</html>
""" % (my_message)
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
sendFrom = 'Purchase Update <admin@cyberkeeda.com>'
sendTo = 'customer@cyberkeeda.com'
# Create the root message and fill in the from, to, and subject headers
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Purchase details Status'
msg['From'] = strFrom
msg['To'] = strTo
msg.attach(part1)
msg.attach(part2)
smtp = smtplib.SMTP('smtp.cyberkeeda.com')
smtp.sendmail(strFrom, strTo, msg.as_string())
smtp.quit()
So modify the above with your requirement.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def trigger_email():
my_message = tabular_table.get_html_string()
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<p>Customer Inventory!<br>
Here are the details?<br>
Link to register. <a href="http://www.python.org">link</a> you wanted.<br>
%s
</p>
</body>
</html>
""" % (my_message)
part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') sendFrom = 'Purchase Update <admin@cyberkeeda.com>' sendTo = 'customer@cyberkeeda.com' # Create the root message and fill in the from, to, and subject headers msg = MIMEMultipart('alternative') msg['Subject'] = 'Purchase details Status' msg['From'] = strFrom msg['To'] = strTo msg.attach(part1) msg.attach(part2) smtp = smtplib.SMTP('smtp.cyberkeeda.com') smtp.sendmail(strFrom, strTo, msg.as_string()) smtp.quit()
So modify the above with your requirement.