
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Send mail from your Gmail account using Python
In this article, we will see how we can 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. It 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. As an example, for google the port is 587.
At first we need to import the module to send mail.
import smtplib
Here 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 some other details.
We are using Google's Gmail service to send mail. So we need some settings (if required) for google's security purposes. If those settings are not set up, then the following code may not work, if the google doesnot support the access from third-party app.
To allow the access, we need to set 'Less Secure App Access' settings in the google account. If the two step verification is on, we cannot use the less secure access.
To complete this setup, go to the Google's Admin Console, and search for the Less Secure App setup.

Steps to Send Mail with attachments using SMTP (smtplib)
- Create MIME
- Add sender, receiver address into the MIME
- Add the mail title into the MIME
- Attach the body into the MIME
- Start the SMTP session with valid port number with proper security features.
- Login to the system.
- Send mail and exit
Example code
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText mail_content = '''Hello, This is a simple mail. There is only text, no attachments are there 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 subject line #The body and the attachments for the mail message.attach(MIMEText(mail_content, 'plain')) #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')
Output
D:\Python TP\Python 450\linux>python 327.Send_Mail.py Mail Sent

- Related Articles
- Send mail with attachment from your Gmail account using Python
- How can I send emails using gmail from my Android application?
- How can I send emails using gmail from my Android application using Kotlin?
- How can I send emails using gmail from my Android application using Kotlin Programming?
- How can I send mail from an iPhone application?
- Write Emails In HTML and then Send Them using Gmail
- Send and archive gmail email in one click
- Ways to delete search history from your account
- Sending an HTML e-mail using Python
- Sending Attachments as an E-mail using Python
- Can Your Netflix Account Be Hacked?
- How to make your Facebook account hackproof?
- How to secure your online gaming account?
- Send SMS updates to mobile phone using python
- Send data from one Fragment to another using Kotlin?
