Skip to content
Buy 10+ on select items — save 10% auto-applied
Free US shipping on orders $35+
Order by 3pm ET — ships same-day from the US
Skip to main content

Raspberry Pi Pico W Gmail: Send Email in MicroPython | ShillehTek

October 22, 2023 395 views

Raspberry Pi Pico W Gmail: Send Email in MicroPython | ShillehTek
Project

Build a Raspberry Pi Pico W MicroPython script that sends Gmail SMTP email using an app password, enabling easy alerts and remote monitoring with ShillehTek.

15 min Beginner1 parts

Video Tutorial

Watch first if you want to see the full Raspberry Pi Pico W email setup using Gmail and an app password.

Project Overview

In this tutorial, you will use a Raspberry Pi Pico W with Gmail (SMTP via app password) to send an email from MicroPython. This is useful for remote monitoring, alerts, communication between devices, and periodic reporting.

  • Time: 15 to 30 minutes
  • Skill level: Beginner
  • What you will build: A MicroPython script that connects the Pico W to Wi-Fi and sends an email through Gmail SMTP

Parts List

From ShillehTek

External

  • Gmail account with Two Factor Authentication enabled
  • Gmail App Password (generated after enabling 2FA)
  • umail.py library (copy into the Pico filesystem)
  • MicroPython environment for the Pico W (plus a way to copy files to the board)
  • Wi-Fi network credentials (SSID and password)

Note: This tutorial uses a Gmail app password (not your normal Gmail password) for SMTP authentication in MicroPython.

Step-by-Step Guide

Step 1 - Enable Two Factor Authentication in Gmail

Goal: Enable 2FA on the Gmail account you will use for SMTP.

What to do: Open your Google account security settings and enable two-factor authentication for the Gmail account you will use.

https://myaccount.google.com/security

After setup, your security page should show that 2-step verification is turned on.

Gmail security settings page showing Two Factor Authentication enabled
Enable 2-step verification in your Gmail security settings.

Expected result: Two Factor Authentication is enabled on the Gmail account.

Step 2 - Generate a Gmail App Password

Goal: Create an app password to use in MicroPython for SMTP login.

What to do: In the same Google account security area, open App Passwords and generate a new app password. The selected device type does not need to match your exact setup.

https://myaccount.google.com/security

Gmail App Passwords screen showing how to generate an app password
Generate an app password after enabling 2FA.

The app password will replace your normal Gmail password for authentication in MicroPython.

Expected result: You have a generated app password ready to paste into your script.

Step 3 - Add umail.py to your Pico W filesystem

Goal: Install the MicroPython email library used by the script.

What to do: Get umail.py and copy its contents into a file named umail.py. Save it in the top-most directory on the Pico W filesystem.

Pico W MicroPython file directory showing umail.py placed in the root folder
Place umail.py in the root directory of the Pico W.

Expected result: umail.py exists on the Pico W so it can be imported by your script.

Step 4 - Use the email script template (MicroPython)

Goal: Connect the Pico W to Wi-Fi and send an email using Gmail SMTP over SSL.

What to do: Copy the following script and replace the placeholder values (email, name, app password, recipient, and Wi-Fi credentials). The script connects to Wi-Fi first, then sends an email using an SMTP object.

Code:

import network
import time
import urequests

import umail

# Internal libs
import constants


# Email details
sender_email = <Your email>
sender_name = <Your name>
sender_app_password = <APP_PASSWORD>
recipient_email = <EMAIL> 
email_subject ='Like, Comment, Subscribe'

def connect_to_internet(ssid, password):
    # Pass in string arguments for ssid and password
    
    # Just making our internet connection
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid, password)
    
    # Wait for connect or fail
    max_wait = 10
    while max_wait > 0:
      if wlan.status() < 0 or wlan.status() >= 3:
        break
      max_wait -= 1
      print('waiting for connection...')
      time.sleep(1)
    # Handle connection error
    if wlan.status() != 3:
       print(wlan.status())
       raise RuntimeError('network connection failed')
    else:
      print('connected')
      print(wlan.status())
      status = wlan.ifconfig()
      
connect_to_internet(<INTERNET_NAME>, <INTERNET_PASSWORD>)

# Send the email
# Connect to the Gmail's SSL port
smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True)
# Login to the email account using the app password
smtp.login(sender_email, sender_app_password)
# Specify the recipient email address
smtp.to(recipient_email)
# Write the email header
smtp.write("From:" + sender_name + "<"+ sender_email+">\n")
smtp.write("Subject:" + email_subject + "\n")
# Write the body of the email
smtp.write("Test Email from Raspberry Pi Pico W")
# Send the email
smtp.send()
# Quit the email session
smtp.quit()

print('Email Sent')

Expected result: After you plug in your variables and run the script, the Pico W connects to Wi-Fi and the console prints Email Sent.

Conclusion

You set up a Raspberry Pi Pico W to send an email through Gmail by enabling 2FA, generating an app password, adding umail.py, and running a MicroPython SMTP script. This approach is useful for simple notifications, alerts, and lightweight reporting from your Pico W.

Want the exact parts used in this build? Grab what you need from ShillehTek.com. If you want help customizing this project or building something similar for your setup, check out our IoT consulting services. Do not forget to like, comment, or subscribe to the channel for more useful information!