send_email_1.py

#!/usr/bin/python3
# ====================================================================
# send an email (using a gmail account)
#
# you must make a modification to the gmail account.
# 1. create a google account if you do not have one or want to use
#    a diffrent account
# 2. log into the google account
# 3. click on 'Manage your Google Account'
# 4. go to 'security'
# 5. make sure 'LESS SECURE AP ACCESS' is on
# ====================================================================

import smtplib
import ssl
from email.message import EmailMessage

subject  = 'Email from Python'
body     = 'This is a test email from Python'
sender   = 'tlwolfe99@gmail.com'
receiver = 'tlwolfe99@gmail.com'

# ---- ask the user for the google accounti's password

password = input('Enter a password: ').strip()

# ---- build an email

message = EmailMessage()       # email object

message['From']    = sender
message['To']      = receiver
message['subject'] = subject

message.set_content(body)

context = ssl.create_default_context()

print('sending email')

with smtplib.SMTP_SSL('smtp.gmail.com',465,context=context) as server:
    server.login(sender,password)
    server.sendmail(sender,receiver,message.as_string())

print('success')