Send mail with attachment from your Gmail account using Python

In this article, we will see how to send email with attachments using Python. To send mail, we do not need any external library. There is a module called smtplib, which comes with Python. It uses SMTP (Simple Mail Transfer Protocol) to send the mail and creates SMTP client session objects for mailing.

SMTP needs valid source and destination email IDs, and port numbers. The port number varies for different sites. For Gmail, the port is 587.

Required Modules

We need to import the following modules ?

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

We are also using the MIME (Multipurpose Internet Mail Extension) module to make it more flexible. Using MIME header, we can store the sender and receiver information and other details. MIME is also needed to set the attachment with the mail.

Gmail Security Settings

We are using Google's Gmail service to send mail. For this to work, you need to configure Gmail's security settings. If these settings are not set up, the following code may not work if Google doesn't support access from third−party apps.

To allow access, you need to set 'Less Secure App Access' settings in the Google account. If two−step verification is on, you cannot use the less secure access.

To complete this setup, go to Google's Admin Console and search for the Less Secure App setup.

Send Mail

Steps to Send Mail with Attachments

  • Create MIME multipart message
  • Add sender, receiver address into the MIME
  • Add the mail subject into the MIME
  • Attach the body into the MIME
  • Open the file as binary mode, which is going to be attached
  • Read the byte stream and encode the attachment using base64 encoding
  • Add header for the attachments
  • Start the SMTP session with valid port number and security features
  • Login to the system
  • Send mail and exit

Example

Here's a complete example to send an email with attachment ?

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders

# Email content
mail_content = '''Hello,
This is a test mail.
In this mail we are sending some attachments.
The mail is sent using Python SMTP library.
Thank You
'''

# The mail addresses and password
sender_address = 'sender123@gmail.com'
sender_pass = 'xxxxxxxx'
receiver_address = 'receiver567@gmail.com'

# Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'

# The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))

# File attachment
attach_file_name = 'TP_python_prev.pdf'
attach_file = open(attach_file_name, 'rb')  # Open file as binary mode
payload = MIMEBase('application', 'octet-stream')
payload.set_payload((attach_file).read())
encoders.encode_base64(payload)  # encode the attachment

# Add payload header with filename
payload.add_header('Content-Disposition', 'attachment', filename=attach_file_name)
message.attach(payload)

# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587)  # use gmail with port
session.starttls()  # enable security
session.login(sender_address, sender_pass)  # login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')
Mail Sent

Key Points

  • Port 587: Used for Gmail SMTP with TLS encryption
  • starttls(): Enables security encryption for the connection
  • MIMEBase: Used for binary file attachments
  • base64 encoding: Required for email attachment transmission
  • Content-Disposition: Header that tells email client this is an attachment
Test Mail

Conclusion

Python's smtplib and email.mime modules provide a straightforward way to send emails with attachments. Remember to configure Gmail's security settings and use proper authentication for successful email delivery.

Updated on: 2026-03-25T05:05:12+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements