Send table in email python

This code sends the message in the typical plain text plus html multipart/alternative format. If your correspondent reads this in an html-aware mail reader, he's see the HTML table. If he reads it plain-text reader, he'll see the plain text version.

Show

In either case, he will see the data included in the body of the message, and not as an attachment.

import csv
from tabulate import tabulate
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

me = ''
password = 'yyyzzz!!2'
server = 'smtp.gmail.com:587'
you = ''

text = """
Hello, Friend.

Here is your data:

{table}

Regards,

Me"""

html = """

Hello, Friend.

Here is your data:

{table}

Regards,

Me

""" with open('input.csv') as input_file: reader = csv.reader(input_file) data = list(reader) text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid")) html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html")) message = MIMEMultipart( "alternative", None, [MIMEText(text), MIMEText(html,'html')]) message['Subject'] = "Your data" message['From'] = me message['To'] = you server = smtplib.SMTP(server) server.ehlo() server.starttls() server.login(me, password) server.sendmail(me, you, message.as_string()) server.quit()

Send table in email python

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 formatHow we can send HTML email using Python.How can we integrate mime with email.Code snippet to send HTML email along with TexrHow to send email as MultiPart message content ( Text + HTML) We will start with the use case:

 

Intended Table.

FirstnameLastnameAgeJill Smith 50 Eve Jackson 94 John Doe 80 How to create Tabular data in CLI using prettytable.

mytable.py (adsbygoogle = window.adsbygoogle || []).push({}); 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.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())
Firstname Lastname Age
Jill Smith 50
Eve Jackson 94
John Doe 80
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.

How do you send a table in the body of an email in Python?

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

How do I email data from python?

Set up a secure connection using SMTP_SSL() and . starttls() Use Python's built-in smtplib library to send basic emails. Send emails with HTML content and attachments using the email package.

How do you send HTML content in an email using python?

Send HTML content with the email..
Import modules. ... .
Define the HTML document. ... .
Set up the email addresses and password. ... .
Create a MIMEMultipart class, and set up the From, To, Subject fields. ... .
Attach the html doc defined earlier, as a MIMEText html content type to the MIME message. ... .
Convert the email_message as string..