Skip to content

Instantly share code, notes, and snippets.

@dealbreaker973
Created January 9, 2021 07:05
Show Gist options
  • Select an option

  • Save dealbreaker973/b1b4ae25d1fbe843c4b338984c1aeca4 to your computer and use it in GitHub Desktop.

Select an option

Save dealbreaker973/b1b4ae25d1fbe843c4b338984c1aeca4 to your computer and use it in GitHub Desktop.
python3 send email via gmail
import smtplib
gmail_user = '' # change this
gmail_password = '' # change this
#(if 2 FA are activated, you need to use app passwords
sent_from = gmail_user
to = ['', ''] # add receiver here
subject = ''
body = ''
email_text = """\
From: {}
To: {}
Subject: {}
{}
""".format(sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.sendmail(sent_from, to, email_text)
server.close()
print('Email sent!')
except Exception as e:
print('Something went wrong...')
print(str(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment