Popup

Wait! Don’t Go Yet! 👋

Become a Member Today and Unlock Access to All eBooks! 😍

Thousands of eBooks at your fingertips. Read, learn, and grow anytime, anywhere ✨

Raspberry Pi: Send an Email using Python (SMTP Server)

In this guide, you’ll learn how to send an email from your Raspberry Pi using a Python Script and SMTP servers. The example we’ll show can also be run on any other machine that runs Python.

Prerequisites

Before proceeding with this tutorial, please verify the following prerequisites.

  1. Get familiar with the Raspberry Pi board—if you’re not familiar with the Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
  2. You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).

Introducing SMTP Servers

SMTP means Simple Mail Transfer Protocol, and it is an Internet standard for email transmission. You can easily send emails using a Python Script on your Raspberry Pi using the smtplib library. This library defines an SMTP client session object that can be used to send emails. Learn more here.

SMTP Server Settings

To send emails using a Python script on your Raspberry Pi, you need a sender email, and you’ll need to know your email SMTP server settings. Below you’ll find the settings for the most popular email providers.

Gmail SMTP Server Settings

Top 6

Raspberry Pi eBooks

From Zero to Professional

Raspberry Pi Projects

If you’re using a Gmail account, these are the SMTP Server details:

  • SMTP Server: smtp.gmail.com
  • SMTP username: Complete Gmail address
  • SMTP password: Your Gmail password
  • SMTP port (TLS): 587
  • SMTP port (SSL): 465
  • SMTP TLS/SSL required: yes

Outlook SMTP Server Settings

For Outlook accounts, these are the SMTP Server settings:

  • SMTP Server: smtp.office365.com
  • SMTP Username: Complete Outlook email address
  • SMTP Password: Your Outlook password
  • SMTP Port: 587
  • SMTP TLS/SSL Required: Yes

Live or Hotmail SMTP Server Settings

For Live or Hotmail accounts, these are the SMTP Server settings:

  • SMTP Server: smtp.live.com
  • SMTP Username: Complete Live/Hotmail email address
  • SMTP Password: Your Windows Live Hotmail password
  • SMTP Port: 587
  • SMTP TLS/SSL Required: Yes

If you’re using another email provider, you need to search for its SMTP Server settings—you’ll easily find them with a quick Google search.

Sender Email (New Account)

We recommend creating a new email account to send the emails to your main personal email address. Do not use your main personal email to send emails via a Python script. If something goes wrong in your code or if, by mistake, you make too many requests, you can be banned or have your account temporarily disabled.

We’ll use a newly created Gmail.com account to send the emails, but you can use any other email provider. The receiver's email can be your personal email without any problem.

Create a Sender Email Account

Create a new email account for sending emails with the ESP32/ESP8266. If you want to use a Gmail account, go to this link to create a new one.

Gmail Create a new account

Create an App Password

You need to create an app password so that the RPi is able to send emails using your Gmail account. An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. Learn more about sign-in with app passwords here.

An app password can only be used with accounts that have 2-step verification turned on.

  1. Open your Google Account.
  2. In the navigation panel, select Security.
  3. Under “Signing in to Google,” select 2-Step Verification > Get started.
  4. Follow the on-screen steps.

After enabling 2-step verification, you can create an app password.

  1. Open your Google Account.
  2. In the search panel, search for App Passwords.
  3. Open the App Passwords menu.
Google account create app password to send email with esp32
  1. Give it a name, for example, Raspberry Pi. Then, click on Create.
Google Creating an App Password for the Raspberry Pi

It will pop-up a window with a password that you’ll use with the Raspberry Pi to send emails. Save that password (even though it says you won’t need to remember it) because you’ll need it later.

Generated app password Google Account

Now, you should have an app password that you can use on your Python script to send emails.

If you’re using another email provider, check how to create an app password. You should be able to find the instructions with a quick Google search “your_email_provider + create app password”.

The Email-Sending Script

Create a new Python file called send_email.py and copy the following code.

You need to insert your sender email details and the recipient email.

# Complete Project Details: https://ebokify.com/raspberry-pi-send-email-python-smtp-server/

import smtplib
from email.message import EmailMessage

#Set the sender email and password and recipient emaiç
from_email_addr ="REPLACE_WITH_THE_SENDER_EMAIL"
from_email_pass ="REPLACE_WITH_THE_SENDER_EMAIL_APP_PASSWORD"
to_email_addr ="REPLACE_WITH_THE_RECIPIENT_EMAIL"

# Create a message object
msg = EmailMessage()

# Set the email body
body ="Hello from Raspberry Pi"
msg.set_content(body)

# Set sender and recipient
msg['From'] = from_email_addr
msg['To'] = to_email_addr

# Set your email subject
msg['Subject'] = 'TEST EMAIL'

# Connecting to server and sending email
# Edit the following line with your provider's SMTP server details
server = smtplib.SMTP('smtp.gmail.com', 587)

# Comment out the next line if your email provider doesn't use TLS
server.starttls()
# Login to the SMTP server
server.login(from_email_addr, from_email_pass)

# Send the message
server.send_message(msg)

print('Email sent')

#Disconnect from the Server
server.quit()

How the Code Works

Continue reading to learn how the code works and what changes you need to make to the script to make it work for you.

You start by importing the libraries you need for SMTP and email-related functions: smtplib and the EmailMessage class from the email.message module.

import smtplib
from email.message import EmailMessage

Next, you create variables for the email address to send from, that email’s app password, and an email address to send to. We suggest you create a second email to send the notifications to your everyday email, because you will be giving less secure apps access to the account you send from.

#Set the sender email and password and recipient emaiç
from_email_addr ="REPLACE_WITH_THE_SENDER_EMAIL"
from_email_pass ="REPLACE_WITH_THE_SENDER_EMAIL_APP_PASSWORD"
to_email_addr ="REPLACE_WITH_THE_RECIPIENT_EMAIL"

Create an EmailMessage() object called msg that will handle the email message properties.

# Create a message object
msg = EmailMessage()

Set the email body on the following lines. You can change it to whatever text you want.

# Set the email body
body ="Hello from Raspberry Pi"
msg.set_content(body)

Then, we set the sender and recipient in the email message properties.

msg['From'] = from_email_addr
msg['To'] = to_email_addr

The following line sets the email subject; you can change it to whatever you want.

msg['Subject'] = 'TEST EMAIL'

Then, you establish communication with an SMTP server. Pass the provider’s SMTP server address as a string as the first argument to smtplib.SMTP(), and the port as an int as the second argument.

server = smtplib.SMTP('smtp.gmail.com', 587)

In this script, we’re using a Gmail SMTP server and port. If you use another email provider, make sure to change those values.

The server.starttls() function is necessary for email providers that use TLS to encrypt messages (which are practically all email providers). If your email provider doesn’t use TLS, you can remove or comment out that line.

server.starttls()

Next, the script logs into the sending email account and sends the email.

# Send the message
server.send_message(msg)

Finally, we stop the communication with the server.

server.quit()

Demonstration

Save your Python file. Then run it on your Raspberry Pi. Run the following command in the directory of your project file (use the name of your file):

python send_email.py

After running the script, you should receive an email from the Raspberry Pi in your email account.

Receive Email from Raspberry Pi
Email-Received-from-Raspberry-Pi

Wrapping Up

In this tutorial, you learned how to send an email with the Raspberry Pi using a Python script. This example is also compatible with other boards/machines that support Python 3.

You might also like taking a look at the following tutorial that shows how to send email notifications when motion is detected:

Do you want to learn more about Raspberry Pi? Take a look at our projects:

Share your love

🚀 Discover the world of electronics and innovation!

✨ Create, program, and experiment with all your creative ideas with ease.

Spotpear

Leave a Reply

Your email address will not be published. Required fields are marked *

Secure Payments
Securing online payments is a shared responsibility, and everyone can contribute.
Free Shipping
You get unlimited free shipping on eligible items with Ebokify, with no minimum spend.
24/7 Support
Sales gifts are helpful tools often used to show appreciation to clients for their purchase.
Gifts & Sales
Our customer care service is offered in the form of 1st or 2nd level support.