How to Send Emails With Python and Google Gmail

by FuzzyPotato in Circuits > Software

84 Views, 0 Favorites, 0 Comments

How to Send Emails With Python and Google Gmail

How to Send Emails With Python and Google Gmail.png

Introduction: How to Send Emails with Python (Using Gmail and SSL)

In this guide, we’ll walk you through setting up a Python script to send emails securely, whether you're working on a local PC or using a cloud-based Python environment like, PythonAnywhere. This flexibility makes it easy to integrate email notifications into any project, from your personal computer to always-on online setups.

We’ll cover both:

  1. Local PC Setup: Perfect if you’re running Python scripts directly on your own machine.
  2. PythonAnywhere Setup: Ideal for cloud-based Python projects or automation without relying on your local device.

By the end of this tutorial, you’ll have a Python script that can send emails directly from your Gmail account, which you can adapt to any project. Whether you're sending alerts, reports, or daily updates from an always-on cloud sever or from your local PC, this setup will make it easy to add email functionality to your Python projects. You’ll be able to use this setup as a core component in any future projects needing email functionality.

Let’s get started!

Supplies

Depending on if you are using your own PC or a cloud server, you'll need:


Your Own PC

  1. You’ll need a personal computer if you prefer running the email script locally.

Python Installed on Your PC

  1. Make sure Python is installed on your computer.

Gmail Account with App Password

  1. Use your Gmail account for sending emails. Follow our steps to set up an App Password for secure access.


An IDE (Integrated Development Environment)

  1. For writing and editing your Python scripts. I'll be using VSCode, but other IDEs like PyCharm or Jupyter Notebooks will also work.

OR

PythonAnywhere Account or Another Cloud Server

  1. For cloud-based email sending, use PythonAnywhere or any other server where you can run Python scripts remotely.

Create an App Password for Gmail Account

Capture1.PNG

Open a google page and click on the user icon

Capture2.PNG

Then click on "manage your google account"

Capture3.PNG

Then the "security" tab

Capture4.PNG

Then activate 2 step verification

Capture5.PNG

Enter your google password

Capture6.PNG

Then click on "App passwords"

Capture6.PNG

Then enter a name for the App password. Then click "Create"

Capture7.PNG

You will then see an App password. Save this as you will not be able to see it again. We will use it in our python script.

The Python Script for Sending Emails

With Gmail set up, we’ll now create the Python script. The code will vary slightly depending on whether you’re running it on your PC or on PythonAnywhere.


A. For Local PC Setup

Here's the code to send an email from your local machine:

import smtplib
import ssl
from email.message import EmailMessage

# Email account credentials
email_sender = 'youremail@gmail.com'
email_password = 'your_app_password_here'
email_receiver = 'receiver@gmail.com'

# Email content
subject = 'Test Email from Python (PC)'
body = 'This is a test email sent from a Python script running on a local PC!'

# Set up the email
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject'] = subject
em.set_content(body)

# Create an SSL context
context = ssl.create_default_context()

# Log in and send the email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.send_message(em)

print("Email sent successfully from your PC!")


B. For PythonAnywhere Setup

On PythonAnywhere, due to permissions, we’ll use certifi to ensure SSL certificates are up to date. Here’s how the code changes:

import smtplib
import ssl
import certifi
from email.message import EmailMessage

# Email account credentials
email_sender = 'youremail@gmail.com'
email_password = 'your_app_password_here'
email_receiver = 'receiver@gmail.com'

# Email content
subject = 'Test Email from Python (PythonAnywhere)'
body = 'This is a test email sent from a Python script running on PythonAnywhere!'

# Set up the email
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject'] = subject
em.set_content(body)

# Create an SSL context with certifi for PythonAnywhere
context = ssl.create_default_context(cafile=certifi.where())

# Log in and send the email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.send_message(em)

print("Email sent successfully from PythonAnywhere!")

Running the Scipts

Now that we have everything set up, it’s time to run the scripts. Here’s how to test your email-sending script on both your local PC and PythonAnywhere.


A. For Local PC Setup

  1. Open Your IDE:
  2. Open your IDE (like VSCode) or any text editor and make sure your script is saved with a .py extension.
  3. Run the Script:
  4. In the terminal or command prompt, navigate to the directory where your script is saved.
  5. Run the script by entering: python your_script_name.py
  6. If everything is set up correctly, you should see the confirmation message: “Email sent successfully from your PC!”
  7. Verify Email Delivery:
  8. Check your Gmail Sent folder to confirm that the email was sent.
  9. If you have access to the delivery email address check the Inbox


B. For PythonAnywhere Setup

  1. Upload Your Script:
  2. On PythonAnywhere, go to the Files tab and upload your Python script if it’s saved on your PC. You can also create the script directly in PythonAnywhere.
  3. Run the Script in a Bash Console:
  4. Open a Bash Console from the PythonAnywhere dashboard.
  5. Navigate to the directory where your script is saved (usually your home directory).
  6. Run the script with: python3 your_script_name.py
  7. You should see “Email sent successfully from PythonAnywhere!” if it works correctly.
  8. Verify Email Delivery:
  9. Check your Gmail Sent folder to confirm that the email was sent.
  10. If you have access to the delivery email address check the Inbox

Congratulations!

Congratulations-Pic-2443762323.png

If you found this helped or was valuable in anyway please drop a like! It helps more than you know!