Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Send Emails Using Python
Next article icon

Send Emails Using Python

Last Updated : 17 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

By using Python, you can send emails which can be a valuable skill for automation, communication, and data-driven processes. In this article, we will explore how to send mail from Gmail using Python.

How can you send Emails using Python?

Python offers a library to send emails- "SMTP" Library. "smtplib" creates a Simple Mail Transfer Protocol (SMTP) client session object which is used to send emails to any valid Email ID on the internet.

Prerequisites

Before starting into the main aspect, we need to make sure about some prerequisites. You need to make sure that Python is already installed in your system. To install Python in your system-

You can Refer to this article - Download and Install Python 3 Latest Version

Additionally, you'll need access to an email account with SMTP (Simple Mail Transfer Protocol) credentials. Most of the email providers such as Gmail or Outlook gives these credentials fo sending mails programmatically.

What is SMTP?

SMTP is a protocol that is used to send emails, and as we know Python provides the 'smtplib' library to interact with it. Begin by importing the library and establishing a connection with your email server. Below are the following steps to send mails:

Step 1: First of all, "smtplib" library needs to be imported.

Step 2: After that create a session, we will be using its instance SMTP to encapsulate an SMTP connection.

s = smtplib.SMTP('smtp.gmail.com', 587)

Step 3: In this, you need to pass the first parameter of the server location and the second parameter of the port to use. For Gmail, we use port number 587.

Step 4: For security reasons, now put the SMTP connection in TLS mode. TLS (Transport Layer Security) encrypts all the SMTP commands. After that, for security and authentication, you need to pass your Gmail account credentials in the login instance. The compiler will show an authentication error if you enter an invalid email id or password.

Step 5: Store the message you need to send in a variable say, message. Using the sendmail() instance, send your message. sendmail() uses three parameters: sender_email_id, receiver_email_id and message_to_be_sent. The parameters need to be in the same sequence.

Code Implementation:

This will send the email from your account. After you have completed your task, terminate the SMTP session by using quit(). 

Python
import smtplib # creates SMTP session s = smtplib.SMTP('smtp.gmail.com', 587) # start TLS for security s.starttls() # Authentication s.login("sender_email_id", "sender_email_id_password") # message to be sent message = "Message_you_need_to_send" # sending the mail s.sendmail("sender_email_id", "receiver_email_id", message) # terminating the session s.quit() 

Send Email to Multiple Recipients using Python

If you need to send the same message to different people. You can use for loop for that. For example, you have a list of email ids to which you need to send the same mail. To do so, insert a "for" loop between the initialization and termination of the SMTP session. Loop will initialize turn by turn and after sending the email, the SMTP session will be terminated. 

Python
import smtplib  # list of email_id to send the mail li = ["[email protected]", "[email protected]"]  for dest in li:     s = smtplib.SMTP('smtp.gmail.com', 587)     s.starttls()     s.login("sender_email_id", "sender_email_id_password")     message = "Message_you_need_to_send"     s.sendmail("sender_email_id", dest, message)     s.quit() 

Some Important Points:

  • One of the most amazing things about this code is that we can send any number of emails using this and Gmail mostly puts your mail in the primary section. Sent emails would not be detected as Spam generally.
  • File handling can also be used to fetch email IDs from a file and further used for sending emails.
  • This code can send simple mail which doesn't have any attachment or any subject.

You can also refer to -  Send mail with attachments from your Gmail account using Python 


Next Article
Send Emails Using Python

K

kartik
Improve
Article Tags :
  • Technical Scripter
  • Python
  • TechTips
Practice Tags :
  • python

Similar Reads

    Send Email Using yagmail in Python
    In this article, we are going to see how to send email using yagmail. yagmail(Yet Another Gmail)  is a module in python which used to send emails using Python. This module is nothing, just a Gmail/SMTP(Simple Mail Transfer Protocol) client that removes the issues of sending emails through Python. Th
    3 min read
    File Sharing App using Python
    Computer Networks is an important topic and to understand the concepts, practical application of the concepts is needed. In this particular article, we will see how to make a simple file-sharing app using Python.  An HTTP Web Server is software that understands URLs (web address) and HTTP (the proto
    4 min read
    Sending Email using FastAPI Framework in Python
    Before jumping into the topic directly, let's have a small intro about the technologies we are going to use. As the name suggests, we will be using FastAPI, a Python language framework. FastAPI: FastAPI is a python framework to develop REST Apis. It is very easy to build,  high performance, easy to
    3 min read
    Send message to FB friend using Python
    The power of Python comes because of the large number of modules it has. This time we are going to use one of those. Every one of us, one time or another, has a wish of the message (or spamming -.-) our Facebook friend. This is a program that can do something similar. So without further delay, let’s
    3 min read
    Tweet using Python
    Twitter is an online news and social networking service where users post and interact with messages. These posts are known as "tweets". Twitter is known as the social media site for robots. We can use Python for posting the tweets without even opening the website. There is a Python library which is
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences