Created
September 9, 2018 14:20
-
-
Save arajajyothibabu/f2d5d7a4f9aca3c8b2bd90e6b96f70ac to your computer and use it in GitHub Desktop.
Sending email using Python and Gmail
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 | |
| from email.mime.text import MIMEText | |
| def send_email(): | |
| from_address = '[email protected]' | |
| to_addresses = [subscriber['email'] for subscriber in subscribers] | |
| print "Sending emails to ", ", ".join(to_addresses) | |
| msg = MIMEText(EMAIL_BODY, 'html') | |
| msg["Subject"] = "Some Subject" | |
| msg["From"] = from_address | |
| msg["To"] = ", ".join(to_addresses) | |
| try: | |
| # set up the SMTP server | |
| server = smtplib.SMTP(host='smtp.gmail.com', port=587) | |
| server.ehlo() | |
| server.starttls() | |
| #s.set_debuglevel(1) | |
| server.login(EMAIL_ADDRESS, EMAIL_PASSWORD) | |
| server.sendmail(from_address, to_addresses, msg.as_string()) | |
| # Terminate the SMTP session and close the connection | |
| server.quit() | |
| print "Mail Sent Successfully." | |
| except: | |
| print "Sending mail failed..!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment