Last modified: Jun 04, 2023 By Alexander Williams
Send Email with Attachment or Multi Attachments in Python
It seems like you're here because you want to learn how to send an email with one or multiple attachments. I'll guide you through the process to ensure you do it effectively.
Sending a mail with an attachment
If you plan to use the Gmail server, there are a few settings you need to enable to proceed. Here's what you need to do:
- Firstly, sign in to your Gmail account.
- Access your account settings in the top-right corner of the Gmail interface.
- Look for the tab labelled "Forwarding and POP/IMAP" and click on it.
- In the "POP Download" section, ensure that either "Enable POP for all mail" or "Enable POP for mail that arrives from now on" is selected. This step allows you to download your emails using POP.
- Scroll down to the "IMAP Access" section and select "Enable IMAP". Enabling IMAP grants you access to your Gmail account through IMAP.
- Once you've made these changes, remember to save them.
Also, you will need to install the smtplib and email libraries:
pip install smtplib email
Now, let's break down the process into manageable steps:
# Importing the Required Libraries
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Setting up the Email Details
sender_email = "your_email@gmail.com"
receiver_email = "recipient_email@example.com"
subject = "Email with Attachment"
body = "Dear recipient,\n\nPlease find the attached document.\n\nBest regards,\nSender"
# Creating the Email Object
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# Attaching the File
attachment_path = "path_to_file/document.pdf"
# Attach the file using the MIMEBase class
attachment = open(attachment_path, "rb")
payload = MIMEBase("application", "octet-stream")
payload.set_payload((attachment).read())
encoders.encode_base64(payload)
payload.add_header(
"Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}"
)
message.attach(payload)
# Establishing the SMTP Connection
smtp_server = "smtp.gmail.com"
smtp_port = 587
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, "your_password")
server.send_message(message)
Note: To ensure account security, it is recommended to store your password securely or use app-specific passwords.
Send multiple files as attachments
To send multiple files as attachments, we need to use for loop that iterates over each attachment path.
Example:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Set up email details
sender_email = "your_email@gmail.com"
receiver_email = "recipient_email@example.com"
subject = "Email with Attachments"
body = "Dear recipient,\n\nPlease find the attached files.\n\nBest regards,\nSender"
# Create the email object
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
# Add the body of the email
message.attach(MIMEText(body, "plain"))
# Attach the files
attachment_paths = ["path_to_file/data.csv", "path_to_file/data.xlsx"]
for attachment_path in attachment_paths:
attachment = open(attachment_path, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", f"attachment; filename= {attachment_path.split('/')[-1]}")
message.attach(part)
# Connect to the SMTP server and send the email
smtp_server = "smtp.gmail.com"
smtp_port = 587
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, "your_password") # Replace with your password or use app-specific passwords
server.send_message(message)
Conclusion
In conclusion, sending an email with attachments in Python can be achieved by utilizing the smtplib and email libraries. By following the steps outlined in this guide, you can successfully send emails with one or multiple attachments.