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

PythonForBeginners.com

Learn By Example

  • Home
  • Python Tutorial
  • Python Basics
  • Python Code Examples


You are here: Home / Code Snippets / Sending emails using Google

Sending emails using Google

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 ([email protected] or [email protected]_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 = '[email protected]'
toaddrs  = '[email protected]'
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:

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial
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 = '[email protected]'
toaddrs  = '[email protected]'

# 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

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Code Snippets Date Originally Published: May 30, 2013

More Python Topics

API Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line crawler Data Types Development Dictionary Dictionary Data Structure In Python envoy Errorhandling Error Handling Exceptions Fabric Files fnmatch ftplib Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pil pip Python Python Code Snippets Python On The Web Python Strings Requests Scraping Scripts sh simplehttpserver System & OS urllib2 Web

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Datacamp Review 2020
  • Most Common Python Interview Questions For 2020
  • Python 2 Vs Python 3 with Examples
  • How To Run Your Python Scripts
  • The 5 Best Python IDE’s and Code Editors for 2019

Python Courses

  • Datacamp: Intro To Python
  • 2021 Complete Python Bootcamp
  • Python Mega Course: Build 10 Real World Apps
  • Python Data Science Bootcamp
  • Complete Python Developer: Zero to Mastery

Copyright © 2012–2021 · PythonForBeginners.com

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