Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Send Email With Python

This repository is for practicing sending email with python.

Requirements

  • Python 3 (only the standard library is used — nothing to install)
  • A Gmail account with 2-Step Verification enabled and an app password generated for it

Configuration

Three environment variables are read at startup. If any is missing, the program prints which one and exits with status 1 before any connection is attempted.

Variable Description
EMAIL_SENDER_ADDRESS The Gmail address the message is sent from
EMAIL_SENDER_APP_PASSWORD The 16-character app password for that account — not the account password
EMAIL_RECIPIENT The address the message is sent to
export EMAIL_SENDER_ADDRESS="you@gmail.com"
export EMAIL_SENDER_APP_PASSWORD="abcdefghijklmnop"
export EMAIL_RECIPIENT="someone@example.com"

Usage

./run.sh

Or without the wrapper script:

python3 src/main.py

Either way the following prompt appears:

Use SSL or TLS? (s/t): 
  • s — connects to smtp.gmail.com:465 over SSL
  • t — connects to smtp.gmail.com:587 and upgrades the connection with STARTTLS
  • Anything else — prints Invalid input! Please type 's' or 't'. and exits without connecting

Both answers log in to Gmail and send a real email containing one randomly chosen message from the list in src/main.py.

If the send fails — an unreachable server, a rejected login, a refused recipient — the reason is printed as Failed to send email: <reason> and the program exits with status 1, so run.sh reports the failure rather than a success.

Tests

The test suite uses the standard library's unittest module and mocks every SMTP connection, so running it sends no email and opens no socket. From the repository root:

python3 -m unittest discover -s tests -t .

Relevant Links

About

This repository is for practicing sending emails with python.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages