Created
December 6, 2018 23:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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