Send an email using Python and Gmail

Ljubiša Moćić
2 min readMar 30, 2020

Send an email as simple as possible

Motivation

I’ve written a simple script for sending emails with just Python and Gmail account about year ago, but in a meantime, it stopped working. I discovered that different credentials are needed now in order to send mail via script.

So I wrote this short guide to explain which configuration is required now and how to make it work again.

Prerequisites

Configure Gmail

  1. Enable 2-step verification
  2. Generate app password
For app password generation choose “Mail” and then “Other” device
Name the app and click “Generate”

And now you will get the window with APP_PASSWORD, which you’ll need to save, since it acts now like a password for access to Gmail via script.

Configure script

  1. Copy the file and save it as send-email-python3-gmail.py
  2. Add config: EMAIL, APP_PASSWORD, RECEIVER, SUBJECT, MESSAGE_TEXT

Run script

python3 send-email-python3-gmail.py

Conclusion

This is the simplest way to send an email without worrying about configuration of some external service like Sendgrid or AWS SES.

I hope it will be useful to someone.

Ping me on @ljmocic if you have any questions!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

If you got so far, you probably found something useful. Please consider supporting me :D

--

--