Automate Your Email Process with Ease!
Sending emails with attachments is a common task in many applications. It allows you to share data and documents easily. In this article, we’ll explore how to send emails with CSV file attachments using Python.📨✨

In this tutorial, we will learn how to send emails with CSV attachments using Python.
📝Introduction
CSV (Comma-Separated Values) is a popular format for storing tabular data. 📈🗃️ It’s widely used for spreadsheets, databases, and data interchange. Sending CSV attachments via email can be particularly useful for sharing reports, data exports, or any other structured information. 📊💼
We’ll use the built-in smtplib and email libraries in Python to accomplish this task. 🔧🔍
🔍 Prerequisites
Before we begin, make sure you have the following prerequisites:
🐍 Python installed on your system.
A valid 📧 email account with SMTP server details.
📤 Sending Emails With CSV Attachment Using Python
from email.mime.multipart import MIMEMultipart # For creating multipart messages 📫
from email.mime.application import MIMEApplication # For attaching files 📎
from email.mime.text import MIMEText # For email body text ✏️
import smtplib # For sending emails 📡
def send_mail():
# Create a multipart message 📨
msg = MIMEMultipart()
body_part = MIMEText(MESSAGE_BODY, 'plain') # Create the email body ✍️
msg['Subject'] = EMAIL_SUBJECT # Set the email subject 🗒️
msg['From'] = EMAIL_FROM # Sender's email address ✉️
msg['To'] = EMAIL_TO # Receiver's email address 📬
# Add body to email
msg.attach(body_part) # Attach the body to the message 📝
# Open and read the CSV file in binary 🔍📄
with open(PATH_TO_CSV_FILE, 'rb') as file:
# Attach the file with filename to the email 🖇️
msg.attach(MIMEApplication(file.read(), Name=FILE_NAME))
# Create SMTP object 📡
smtp_obj = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) # Connect to the SMTP server 🌐
# Login to the server 🔑
smtp_obj.login(SMTP_USERNAME, SMTP_PASSWORD)
# Convert the message to a string and send it 💌
smtp_obj.sendmail(msg['From'], msg['To'], msg.as_string()) # Send the email ✈️
smtp_obj.quit() # Close the SMTP session 🔒
📋 Variables:
EMAIL_SUBJECT = Subject of the email ✏️🗒️
EMAIL_FROM = Sender’s email address ✉️
EMAIL_TO = Receiver’s email address 📫
MESSAGE_BODY = Email body text 📝
PATH_TO_CSV_FILE = Path to the CSV file 📁
FILE_NAME = Filename for the email attachment 📎
SMTP_SERVER = SMTP server 🌐
SMTP_PORT = SMTP port 🔌
SMTP_USERNAME = Username for SMTP 🔑
SMTP_PASSWORD = SMTP password 🔒
Since these variables include sensitive data, I always recommend feeding them from environment variables. 🌱 However, you can also pass them as arguments to the send_mail
function. 🛠️
🛠️ Explanation
First, we are importing all the necessary modules for the function. 📥 Then, inside the function, we are creating a multipart message using the MIMEMultipart object. Since the multipart object takes arguments in key-value pairs, we are providing all the necessary arguments such as the message body, sender’s address, and receiver’s address in the same manner. 💬📧
Next, we are opening the CSV file in binary form and attaching the binary stream to the email using the MIMEApplication method. 📂🖇️
Then we are using Python’s built-in SMTP module to create an SMTP session object by providing the login credentials and finally sending the mail. 📮💻
Note: In case you want to send multiple CSV files in the same mail, you can create a list of files and loop over them to attach them to the email like this:
# Create file list 🗂️
csv_files = ['path_to_csv_1', 'path_to_csv_2'] # List of CSV files 📄📄
# Loop over files 🔄
for file in csv_files:
with open(file, 'rb') as file: # Open each CSV file in binary 🔍
# Attach the file with filename to the email 🖇️
msg.attach(MIMEApplication(file.read(), Name=FILE_NAME)) # Attach to email 📤
🏁 Conclusion
Sending emails with CSV attachments using Python is a straightforward process. 🛤️ The smtplib and email libraries provide the necessary tools to compose and send emails with attachments. Whether you need to share reports, data exports, or any other structured information, this approach allows you to automate the process and enhance communication with your recipients. 📈💬
- Drop a Clap 👋 and share your thoughts 💬 below!
- Follow me here on Medium.
- Join my email list so you never miss another article.