• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / API / Send Email Using Python

Send Email Using Python

Author: Aditya Raj
Last Updated: November 7, 2022

Emails are one of the most important parts of our lives. In this article, we will discuss how we can send an email using python. 

How to Send E-Mail Using Python?

In this article, we will use Simple Mail Transfer Protocol (SMTP) to send emails using python. For this, we will use the smtplib module. Also, we will use a gmail.com email id to send the email. We need to specify the mailing platform because different mailing platforms use different port numbers to send the email. Therefore, we need to know the port number used by the mail service providers for sending the email using python. 

Suggested Reading: Create Chat Application in Python

Steps to Send Email Using the smtplib Module in Python

For sending the mail using the smtplib module, we will follow the following steps.

  • First, we will import the smtplib module using the import statement.
  • After that, we will create a session using the SMTP() method. The SMTP() method takes the mail server location as its first input argument and the port number as its second input argument. Here, we will pass ‘smtp.gmail.com’ as the mail server location and 587 as the port number. After execution, the SMTP() method creates an SMTP session.
  • We will use Transport Layer Security (TLS) in our SMTP session. For this, we will invoke the starttls() method on the session object returned by the SMTP() method.
  • After starting TLS, we will log in to our Gmail account. For this, we will use the login() method. The login() method, when invoked on a session object, accepts the username as its first input argument and the password as its second input argument. If the username and password are correct, you will be signed in to Gmail. Otherwise, the program will run into an error.
  • After logging in, we will send the email using the sendmail() method. The sendmail() method, when invoked on a session object, takes the sender’s email address as its first input argument, the receiver’s email address as its second input argument, and the message to be sent as the third input argument. After execution, the email is sent to the receiver’s email address.
  • After sending the email, we will terminate the SMTP session by invoking the quit() method on the session object.

Following is the code to send email using python.

import smtplib
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
email_id="xxxxx[email protected]" #your email
password= "*********" password of gmail account
s.login("email_id", "password")
message= "Email Body"
s.sendmail("email_id", "receiver_email_id", message)
s.quit()

Conclusion

In this article, we have discussed how to send email using python. To learn more about python programming, you can read this article on dictionary comprehension in python. You might like this article on list comprehension in python too.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: API Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us