Skip to content

Instantly share code, notes, and snippets.

@royz
Last active November 22, 2020 16:30
Show Gist options
  • Save royz/8ea069f7c77f47c5495778fa4f7ce90e to your computer and use it in GitHub Desktop.
Save royz/8ea069f7c77f47c5495778fa4f7ce90e to your computer and use it in GitHub Desktop.
Send email via gmail with Python3
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