Python Script To Send Email Via Gmail

Posted on

Python Script To Send Email Via Gmail – In this article, I’m going to teach you something fun. I’ll teach you how to send an email!

Don’t get me wrong. I’m not going to teach you how to log into your gmail and send an email to your best friend.

Python Script To Send Email Via Gmail

Python Script To Send Email Via Gmail

Suppose, this database stores the names of your customers as well as the products they are interested in based on their past purchases.

How To Send Beautiful Emails With Python — The Essential Guide

Now you want to send personalized emails to each of your customers, addressing them by name and introducing new products that might interest them.

This is where programming comes into play as you can programmatically send emails to each of them with a dynamic body. So instead of writing thousands of emails by hand, write a few lines of code and you’re good to go.

Instead of rushing out and showing you the code, it’s better if you first learn a little about the theory behind how email works.

It’s actually quite simple. It is simply a set of rules that determine how two email servers can communicate with each other.

Gmail Smtp · Github Topics · Github

That said, you don’t need to know how SMTP works to send email using Python, but it’s very valuable.

Python provides you with the smtplib module which removes all the complexities of SMTP. This module essentially implements the SMTP protocol for you. So all you have to do is instruct this module to send an email, sit back, relax and watch smtplib do all the heavy lifting for you.

Before I start talking about this amazingly simple way to send emails using Python, I’d like to start by showing you the code to give you an idea of ​​how simple, short, and lightweight the code really is.

Python Script To Send Email Via Gmail

This code assumes python3 and you have a gmail email account, but the same concepts will work for any email service.

Spring Clean Your Gmail Account With Python

The code represents a client application that communicates with your email server (running at and requests that the server send an email to the email address “[email protected]” with “This message is from python”.

If you take the code I wrote above and change the login information and try to run it immediately with your specific information, you will get an error like this.

By default, Gmail tries to secure your email by preventing this type of access from third parties. You can manage your Gmail security settings by going to this link and enabling less secure apps. It is off by default so you have to turn it on.

Now listen to me. I strongly recommend that you do not allow less secure apps to connect to your gmail as this may expose you to some security attacks.

Sending An Email With Python

So here’s what I want you to do: you should only allow less secure apps to test and experiment with the code, but when you’re done, don’t forget to reset your security settings to their defaults.

With security relaxed, try running the code again with the correct login credentials and the correct destination email address (you may want to send this email to yourself so you can verify that the code actually worked).

I used the code above to email myself and this is what I got in my inbox.

Python Script To Send Email Via Gmail

The smtplib python module defines an SMTP client object that can be used to send email to any machine running an SMTP server.

Using Python, Gmail Smtp To Automate Personalised Email Blast

In our case, the machine running the SMTP server is and we want our client application (running on our laptop) to be able to communicate with that server.

You can use smtplib.SMTP or smtplib.SMTP_SSL to create a client object. The difference is that smtplib.SMTP_SSL uses the secure encrypted SSL protocol to connect to the SMTP server while smtplib.SMTP does not.

Gmail does not allow communication over channels other than SSL or TLS, so we cannot use smtplib.SMTP to communicate with gmail.

We will need some of these settings in order for our client to properly communicate with the SMTP server at gmail.

Python Send Email Via Smtp

SMTP Server Address: This is the IP address or domain name of the machine running the SMTP server. For gmail, this address is

SSL Required: This indicates whether the SMTP server requires communication over a secure encrypted SSL channel. This is a requirement for Gmail. That’s why we use smtplib.SMTP_SSL instead of smtplib.SMTP

As we all know, gmail naturally asks our username and password, so authentication is required in our case.

Python Script To Send Email Via Gmail

Port for SSL: This is the port on which the SMTP server listens. The port number (465) along with the SMTP server address ( uniquely identifies the full address of the SMTP server so that our client code can communicate with it.

How To Send Emails Using Python (updated For 2021)

The difference between a server address and a port number is that a server address will only point you to the machine running the SMTP server.

However, this machine runs several applications, but only the SMTP server will listen on port 465.

And if you want to increase your understanding of networking concepts once and for all, I highly recommend this book by Ross and Kurose. The book is written in a very interesting manner and is very easy to understand.

I hope the code makes more sense to you by now. The arguments to the SMTP_SSL() function are the SMTP server address and port number.

Accessing Gmail Inbox Using Python Imaplib Module

This function tries to connect to the SMTP server located at and is listening on port 465 over a secure encrypted SSL channel. Returns the smtp object pointed to by the variable named server.

We know from earlier discussion that gmail requires authentication. If you try to send an email without first authenticating, the gmail server will return an authentication error.

Needless to say, you need to replace the method arguments in the code above with your own credentials.

Python Script To Send Email Via Gmail

Smtp objects have another method called sendMail which essentially makes a request to the SMTP server to send an email. Sending takes a source email address, a destination email address, and the content of the email message.

Sending Emails In Python [2022 Guide With Code Examples]

In your Gmail inbox, after you use the above code to send the email, you will see that the subject of the email is missing.

Check your email library. It will make your life easier when working with e-mail. You can achieve your goals without any additional modules, but that requires you to delve a little deeper into the SMTP protocol.

Karim received his doctorate in computer science from the University of California, Santa Barbara. He has more than three years of experience teaching CS to students, more than 5 years of research experience and currently works for a Fortune 100 company. He is interested in distributed systems, machine learning, fitness and soccer Share on Twitter Share on LinkedIn Share on Reddit Hacker News Share on Facebook Share on Mastodon

Email is a common way of formal communication today, which is also good for easy file transfer from one person to another. Almost every person who knows or uses the Internet has their own email ID, be it Gmail or Outlook.

Sending Emails Using Python And Gmail

One of the coolest things you can do in Python is the ability to send or receive email. Python programming

The library can be used to send emails or to list all emails in your email account. You can also perform some basic actions, like marking mail as read, using Python.

Mail servers are basically servers used to manage email, Outlook, Gmail, Yahoo, Hotmail etc. have their own mail servers that manage their email services. Mail servers can be further classified into two categories;

Python Script To Send Email Via Gmail

SMTP stands for Simple Mail Transfer Protocol. This server is responsible for sending or transferring mail from server to server, so when you send mail to someone, you usually use an SMTP server.

How To Send Gmail With Python Step By Step Guide

IMAP stands for Internet Message Access Protocol. This server is responsible for storing and listing emails from your server, so when you open your Gmail or Outlook, you’re usually using an IMAP server.

Whenever you connect to any email server, you will be connecting through one of these protocols. Each protocol has its own port assigned to the server.

Although Gmail and Outlook servers support both protocols, in this post we will only use the TLS protocol for simplicity.

Now that we are clear about the email server, let’s create our first script to send mail using Python

How To Use Python To Send Emails

We will use smtplib python library to send Gmail or Outlook mail. This library is built into Python, so you don’t need to download it from anywhere else.

The script may seem complicated but we will go through the line to discuss each function and class of the script and understand its use.

The script starts by importing the smtplib and getpass libraries. We use the GetPass library to recover passwords

Python Script To Send Email Via Gmail

Then, we ask the user to enter the credentials for his mail account that will be used for sending

Sending Email Using Python And Smtplib

Send email via script, python send email via gmail, send email via mailchimp, send fax via email, python script to send email, send efax via email, send email via gmail, python gmail send email, send email via python, secure email via gmail, gifts to send via email, send email python script