I wanted an easy way to send emails for my Python projects, with code that was
adapted for today's (2021) way of doing things. This Handler
is designed for
use with the Python standard library.
- Python 3.6+ (the currently supported versions of Python)
pip install ssl-smtp-handler
Gmail is probably the easiest to set up. For example:
from logging import basicConfig, info
from ssl_smtp_handler import SSLSMTPHandler
handler = SSLSMTPHandler(
mailhost='smtp.gmail.com',
fromaddr='username@gmail.com',
toaddrs=['to@gmail.com'],
subject='Example subject',
credentials=('username@gmail.com', 'password'),
)
handler.setLevel('INFO')
basicConfig(handlers=[handler], level='INFO')
info('This is an example message')
To try this for yourself, get hold a Gmail account and enable 2-Step verification:
Create an app password:
Name it whatever you want:
Use this password in your program: