Skip to content

Instantly share code, notes, and snippets.

@AO8
Created December 6, 2018 23:04
Show Gist options
  • Select an option

  • Save AO8/c5a6f747eeeca02351152ae8dc79b537 to your computer and use it in GitHub Desktop.

Select an option

Save AO8/c5a6f747eeeca02351152ae8dc79b537 to your computer and use it in GitHub Desktop.
Sending gmail with Python. This version uses the ssl module and SMTP_SSL() from the smtplib module.
import smtplib
import ssl
from email.mime.text import MIMEText
sender = "[email protected]"
receiver = "[email protected]"
password = input("Enter password: ")
text = """\
Hi!
This is a test email send with Python.
Have a great day!"""
message = MIMEText(text)
message["Subject"] = "Sending email with Python!"
message["From"] = sender
message["To"] = receiver
# create a secure SSL context
context = ssl.create_default_context()
# using a 'with' context manager will make sure the connection
# is automatically closed at the end of the indented code block
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as s:
s.login(sender, password)
s.sendmail(sender, receiver, message.as_string())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment