How To Send A .CSV File From Pandas Via Email

Today, you will learn about sending .csv file from pandas by email. While doing analysis on a particular dataset, sometimes we have to share the results with our manager/stakeholder every day or perhaps every week. Rather than downloading the computed dataset as a .csv file, we can simply email the same from pandas using the steps given below.

But before you begin to understand about sending .csv file from pandas by email, you need to install an SMTP Library which will help you in this process.

pip install secure-smtplib

…but what is SMTP?

It stands for Simple Mail Transfer Protocol. It is a protocol that handles email transmission and routing between mail servers.

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet connected machine that has an SMTP or Extended Simple Mail Transfer Protocol (ESMTP) listener daemon.

Let’s import the libraries:

import smtplib
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart from email.mime.text
from email import encoders
from= “email id of sender”
to= ” email id of receiver”

The from variable is used to get the sender’s email address and the to variable is used to get the recipient’s email address.

message = MIMEMultipart()

To begin, we first construct an instance of MIMEMultipart, which will be named “message”.

message[‘From’] = from
message[‘To’] = to
message[‘Subject’] =”Hey , how are you?”
body_email = “Excel file attached”

The sender’s mail id will be invoked to message [‘From’], and the receiver’s mail id will be invoked to message [‘To’]. The body_email variable will hold the actual message that will be displayed.

message.attach(MIMEText(body_email, 'plain'))

The message variable uses method attach in this line, which accepts the first value as the body variable and the second value as the plain value, which converts the encoded value to plain text.

filename = “abc.csv”
attachment = open(“C:\\..\\”+file_name, “rb”)

Making a filename variable and passing the filename you want to send is the first step in the attachment scenario.

After that, we need to create a variable with the method open in it as an attachment.

And “rb” is used to open the file in binary format as read-only and to begin reading from the beginning.

x = MIMEBase(‘application’, ‘octet-stream’)
x.set_payload((attachment).read())
encoders.encode_base64(x)

Here, we have to call MIMEBase in new variable x under that the value has passed as ‘application’ and  ‘octet-stream’. After that, we have changed it to encoded form.

x.add_header(‘Content-Disposition’, “attachment; filename= %s” % filename)
message.attach(x)

Continuing with this article on sending .csv file from pandas by email, you should know that “Content-Disposition” is a response-type header field that gives information on how to process the response payload and additional information such as filename when a user saves it locally.

x.add header is used to set the filename parameter under that Content-Disposition. The attachment filename in %s, which is the string, will be supplied after that. The message .attach method is used to attach instances of up to instance “message”.

s_e = smtplib.SMTP(‘smtp.gmail.com’, 587)
s_e.starttls()

After that, we must build a session, and we’ll encapsulate an SMTP connection using its instance SMTP. You must give the first argument of the server location and the second parameter of the port to use in this instantiation. The port number is 587 (for Gmail).

s_e.login(from, “SENDER PASSWORD”)
text = message.as_string()
s_e.sendmail(from, to, _text)
print(“sent”)

We must now do authentication from the sender’s perspective. This variable will take two values: the sender’s email id and the sender’s password. After that, the text variable will turn the Multipart msg into a string. And the value of fromaddr, toaddr, and text will be used by s_e.sendmail to send the message.

s_e.quit()

This will terminate the SMTP session.

Summary

To summarize: in this article, we’ve completed our e-mail sending concept, which includes attachments and basic configurations using SMTP library and various functions.


References

Documentation for SMTP

Recommended Books:

The books above are affiliate links which benefit this site.