GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Learn Python
  • Python Tutorial
  • Basics
  • Lists
  • Dictionary
  • Code Snippets
  • Comments
  • Modules
  • API
  • Beautiful Soup
  • Cheatsheet
  • Games
  • Loops

This view

  • Original page
  • New address

GeezerVison is showing www.pythonforbeginners.com — Send Email Using Python - PythonForBeginners.com

You are here: Home / API / Send Email Using Python

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="[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

More Python Topics

API Argv Basics Beautiful Soup 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 formatting Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scripts Split Strings System & OS urllib2
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.