Sending Emails with Attachments
Using Python, you can programmatically send emails that include attachments.
The main library used to add attachments to an email is email
.
The email
module provides various classes and methods that allow you to compose an email's content and attachments.
Adding Attachments to an Email
As discussed in a previous lesson, an email consists of Header
and Body
.
The header contains metadata (sender, recipient, subject, etc.) of the email, while the body contains the actual content and attachments.
To attach a file to an email, you use the MIMEBase
object.
Below is the basic code structure for composing an email with an attachment.
# Import necessary libraries
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Email contents setup
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient_email@example.com'
msg['Subject'] = 'Email with Attachment'
# Add attachment
filename = 'example.pdf'
# Read the file to be attached and convert it to MIMEBase
attachment = open(filename, 'rb')
# Convert the file to MIMEBase and add to the email
part = MIMEBase('application', 'octet-stream')
# Set the attachment payload
part.set_payload(attachment.read())
# Encode the attachment
encoders.encode_base64(part)
# Set the attachment's header
part.add_header('Content-Disposition', f'attachment; filename={filename}')
# Attach the file to the email
msg.attach(part)
Code Explanation
1. Email Contents Setup
First, create a MIMEMultipart
object to create a basic email message that can include both the body and attachments.
# Email contents setup
msg = MIMEMultipart()
msg['From'] = 'your_email@example.com'
msg['To'] = 'recipient_email@example.com'
msg['Subject'] = 'Email with Attachment'
Assign the sender, recipient, and subject.
2. Adding an Attachment
MIMEBase is an object that represents the file attachment in the email. Read the file and convert it to MIMEBase, then add it to the email.
# Add attachment
filename = 'example.pdf'
# Read the file to be attached and convert it to MIMEBase
attachment = open(filename, 'rb')
# Convert the file to MIMEBase and add to the email
part = MIMEBase('application', 'octet-stream')
3. Encoding the Attachment
# Set the attachment payload
part.set_payload(attachment.read())
# Encode the attachment
encoders.encode_base64(part)
# Set the attachment's header
part.add_header('Content-Disposition', f'attachment; filename={filename}')
Use the set_payload
method to add the attachment to the MIMEBase object and the encode_base64
method to encode the attachment in base64.
base64 is an encoding scheme that converts data into a string consisting of 64 characters.
Finally, use the add_header
method to set the attachment's filename.
Practice
In the code editor's practice section, make sure to enter your own email into receiver_email
and check if it is well reflected before running the code.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.