CyberKeeda In Social Media

Python - Send HTML Table as email body.



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.


FirstnameLastnameAge
JillSmith50
EveJackson94
JohnDoe80


How to create Tabular data in CLI using prettytable.


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)
+-----------+----------+-----+
| 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.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>
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.


  • 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.



6 comments:

  1. This code example worked great for what I'm trying to do. There are some changes in the above that need to be made, such as "strFrom" and "strTo". Those should be sendFrom and sendTo. I also replaced the last 3 lines with 1) server = smtplib.SMTP_SSL(smtp_server, smtp_port), 2) server.ehlo(), 3) server.login(from_mail, from_password), 4) server.sendmail(from_mail, [from_mail, to_mail], msg.as_string()), and 5) server.quit(). Here, smtp_port is often set to 465 while smtp_server is set to smtp.gmail.com.

    What would be a good addition to the above example is adding html code that controls the color or font style.

    ReplyDelete
  2. Here is an example of one that additionally incorporates a video and visual illustrations for those that need to skim for significant data.gmx net

    ReplyDelete
  3. We should continue to utilize our illustration of 1000 messages and 1000 virtual entertainment posts. best temp mail

    ReplyDelete
  4. Talk with a guaranteed mentor or read pertinent books whenever required; address your doctor on the off chance that you have any exceptional necessities/conditions. Put forth reasonable objectives and draw up a point by point plan for your body building program - screen it as you come. depression mental health tattoos

    ReplyDelete
  5. At the point when you need to assemble a desk yourself, the chances are it won't remain together lengthy.
    https://ergodesks.co

    ReplyDelete

Designed By Jackuna