Last active
November 22, 2020 16:30
-
-
Save royz/8ea069f7c77f47c5495778fa4f7ce90e to your computer and use it in GitHub Desktop.
Send email via gmail with Python3
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 config | |
import smtplib | |
from email.mime.text import MIMEText | |
from email.mime.multipart import MIMEMultipart | |
def send_mail(subject, message): | |
try: | |
message = MIMEMultipart() | |
message['From'] = config.sender_email | |
message['To'] = config.recipient_email | |
message['Subject'] = subject | |
message.attach(MIMEText(message, 'plain')) | |
with smtplib.SMTP('smtp.gmail.com', 587) as session: | |
session.starttls() | |
session.login(config.sender_email, config.sender_password) | |
session.sendmail(config.sender_email, config.recipient_email, message.as_string()) | |
print(f'email sent to {config.recipient_email}') | |
except: | |
print(f'could not send email to {config.recipient_email}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment