Send email via Gmail from Python

Note: This test should only be used with a new throwaway Gmail account as it risks security of the Gmail account in use. Instead consider Oauth with Gmail.


This is a complete example of SMTP sending email via Gmail from Python. To use with two-factor authentication account requires a Gmail App Password.

You need to use Oauth instead of this method for real-world systems, this is just a simple didactic example.

"""
send text string (e.g. status) via Gmail SMTP
"""
import smtplib
from email.mime.text import MIMEText
from getpass import getpass

def sender(user:str, passw:str, to:list, textmsg:str, server:str):
    """
    this is not a good way to do things.
    Should use Oauth.
    """
    with smtplib.SMTP_SSL('smtp.gmail.com') as s:
        s.login(user, passw)

        msg = MIMEText(textmsg)

        msg['Subject']= 'System status update'
        msg['From']= user
        msg['To'] = ', '.join(to)

        s.sendmail(user,to, msg.as_string())
        s.quit()

if __name__ == '__main__':
    from argparse import ArgumentParser
    p = ArgumentParser()
    p.add_argument('user',help='Gmail username')
    p.add_argument('to',help='email address(es) to send to', nargs='+')
    p.add_argument('-s','--server',help='SMTP server',default='smtp.gmail.com')
    p = p.parse_args()

    testmsg="just testing my email from Python setup"

    sender(p.user+'@gmail.com',
           getpass('gmail password: '),
           p.to,
           testmsg,
           p.server)