DEV Community

Cover image for How to send emails with Python? Simply explained for beginners
Aahnik Daw
Aahnik Daw

Posted on

How to send emails with Python? Simply explained for beginners

Background

Sending emails is one of the most common functionalities in the modern world. This article is not about how email works under the hood, but how you can easily send an email using a python script.

Why use Python to send an email when you can use Gmail?

  • when you know how to send an email with python, you can automate a lot of boring stuff
  • you can also integrate the email feature with your other applications
  • send out your newsletter for free

and the use cases are endless.

Requirements

To follow this tutorial you must have python 3.6 + installed in your system. At the time I am writing this article, I recommend using python 3.9.3 (the latest stable version).

Open your terminal (also called command prompt in windows), and type python --version, and you should get something like this.

❯ python --version
Python 3.9.0+
Enter fullscreen mode Exit fullscreen mode

You also need to have an email server running. I will not delve deep into behind-the-scenes technology in this article. If you have a Gmail account (or any other email provider), then you are good to go.

Introducing smtplib

The Simple Mail Transfer Protocol is an internet standard communication protocol for electronic mail transmission. Mail servers and other message transfer agents use SMTP to send and receive mail messages.

Python provides smptplib which is a library used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

As smtplib is a part of the python standard library, you don't have to install anything to start using it.

Python offers another standard library called email for basic email-related operations.

Sending a simple email

So first of all let's import smptplib which has already done all the heavy lifting for us.

from smtplib import SMTP
Enter fullscreen mode Exit fullscreen mode

Now we need to define some important variables to get started.

HOST = 'smtp.gmail.com' 
# use the smtp server address of your mail provider


PORT = 587 
# this if for TLS, 
# will discuss more about this in a future article

SENDER = 'youremail@gmail.com'

PASSWORD = 'your12434(893**!@password' 
# don't share this script with anyone 🤫
# for building real applications
# always read secrets from environment variables
Enter fullscreen mode Exit fullscreen mode

Now that we have defined the variables required, let's move on to start a connection with the mail server and log in with our credentials.

server = SMTP(host=HOST, port=PORT)
# create an SMTP server object

server.connect(host=HOST, port=PORT)
# connect 


server.ehlo()
# extended hello; like saying hello

server.starttls()
server.ehlo()

server.login(user=SENDER, password=PASSWORD)
# login with your credentials

Enter fullscreen mode Exit fullscreen mode

All the shitty stuff is now over, it's time to finally send the email.

Before we do that, we need to have the email address of the recipient and also the message we want to send them.

RECIPIENT = 'email@example.com'

MESSAGE = '''
Hi! How are you?

This is my first email sent from Python.

Hope to see you soon.
'''
Enter fullscreen mode Exit fullscreen mode

Finally, one more line to send the email to the destination.

server.sendmail(SENDER,RECIPIENT,MESSAGE)
Enter fullscreen mode Exit fullscreen mode

And that's it, we are done. 😀

Reading the tutorial passively will do you no good. Write down the script in a file called send.py and run it.

❯ python send.py
Enter fullscreen mode Exit fullscreen mode

If everything went right, then there will be no output in the terminal, and you will see the email sitting right in the recipient's inbox.

In the upcoming articles of this series, we will make some enhancements to the script, so that we can send rich text emails and also attach files with our email.

Top comments (8)

Collapse
 
aahnik profile image
Aahnik Daw

Hi! readers, I am @aahnik on dev.to as well as Github. you may follow me, to stay updated with the content I post.

Collapse
 
ironcladdev profile image
Conner Ow

Does this work without turning on less-secure-apps in chrome??

Collapse
 
aahnik profile image
Aahnik Daw • Edited

a very good question. i think you are the first one to ask.

use an app password, instead of your actual password

support.google.com/accounts/answer...

create an app password, with any name (lets say "email app"), and use the code given by google instead of your real password

Collapse
 
ironcladdev profile image
Conner Ow

I've done that.

Collapse
 
aahnik profile image
Aahnik Daw

More articles coming soon, in this emailing with python series...

Collapse
 
aahnik profile image
Aahnik Daw
Collapse
 
aahnik profile image
Aahnik Daw

GitHub logo aahnik / tgcf

The ultimate tool to automate telegram message forwarding.

tgcf logo

tgcf

The ultimate tool to automate telegram message forwarding

GitHub license GitHub stars GitHub issues PyPI Twitter


The key features are:

  1. Two modes of operation are past or live for dealing with either existing or upcoming messages.
  2. Supports signing in with both telegram bot account as well as user account.
  3. Custom Filtering of messages based on whitelist or blacklist.
  4. Modification of messages like Text Replacement Watermarking, OCR etc.
  5. Detailed documentation📖 + Video tutorial + Fast help in discussion forum💬.
  6. If you are a python developer, writing plugins🔌 is like stealing candy from a baby.

What are you waiting for? Star 🌟 the repo and click Watch 🕵 to recieve updates.

You can also join the official Telegram Channel, to recieve updates without any ads.

Video Tutorial 📺

A youtube video is coming soon. Subscribe to get notified.

Run Locally 🔥

Note: Make sure you have Python 3.8 or above installed Go…

Collapse
 
aahnik profile image