Last active
January 17, 2023 18:15
-
-
Save ryanlong1004/bbeee25af90d3df8bc15d60a7fbfff0a to your computer and use it in GitHub Desktop.
Python3 Gmail snippet
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 json | |
| import os | |
| import pathlib | |
| import smtplib, ssl | |
| ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| config = json.load(open(pathlib.Path(ROOT_DIR, "./config.json"), "r+")) | |
| config["root_dir"] = os.path.dirname(os.path.abspath(__file__)) | |
| def send_email(subject, text, password): | |
| """ | |
| Send an email with gmail. | |
| """ | |
| port = 465 # For SSL | |
| smtp_server = "smtp.gmail.com" | |
| sender_email = "<add_sender_here>" # Enter your address | |
| receiver_email = "<add_receiver_here>" # Enter receiver address | |
| context = ssl.create_default_context() | |
| with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: | |
| server.login(sender_email, password) | |
| server.sendmail(sender_email, receiver_email, f"Subject: {subject}\n\n{text}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment