• 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 / Code Snippets / Sending emails using Google

Sending emails using Google

Author: PFB Staff Writer
Last Updated: May 24, 2020

Overview

A common task for system administrators and developers is to use scripts to send emails if an error occurs.

Why use Gmail?

Using Googles SMTP servers are free to use and works perfectly fine to relay emails. Note that Google has a sending limit: “Google will temporarily disable your account if you send messages to more than 500 recipients or if you send a large number of undeliverable messages. ” As long as you are fine with that, you are good to go.

Where do I start?

Sending mail is done with Python’s smtplib using an SMTP (Simple Mail Transfer Protocol) server. Since we will use Google’s SMTP server to deliver our emails, we will need to gather information such as server, port, authentication. That information is easy to find with a Google search.

Google’s Standard configuration instructions

Outgoing Mail (SMTP) Server – requires TLS or SSL: smtp.gmail.com

Use Authentication: Yes

Port for TLS/STARTTLS: 587

Port for SSL: 465

Server timeouts: Greater than 1 minute, we recommend 5

Account Name or User Name: your full email address (including @gmail.com or @your_domain.com)

Email Address: your email address (username@gmail.com or username@your_domain.com) Password: your Gmail password

Getting Started

Begin with opening up your favorite text editor and import the smtplib module at the top of your script.

import smtplib

Already at the top we will create some SMTP headers.

fromaddr = 'fromuser@gmail.com'
toaddrs  = 'touser@gmail.com'
msg = 'Enter you message here’

Once that is done, create a SMTP object which is going to be used for connection with the server.

server = smtplib.SMTP("smtp.gmail.com:587”)

Next, we will use the starttls() function which is required by Gmail.

server.starttls()

Next, log in to the server:

server.login(username,password)

Then, we will send the email:

server.sendmail(fromaddr, toaddrs, msg)

The final program

You can see the full program below, by now you should be able to understand what it does.

import smtplib
# Specifying the from and to addresses

fromaddr = 'fromuser@gmail.com'
toaddrs  = 'touser@gmail.com'

# Writing the message (this message will appear in the email)

msg = 'Enter you message here'

# Gmail Login

username = 'username'
password = 'password'

# Sending the mail  

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
More Reading

Using Python to send email

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: Code Snippets Author: PFB Staff Writer

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 Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark 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 Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

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