Created
June 26, 2018 17:22
-
-
Save pybokeh/21e6338c0fa2bc82c239ad5b7dd0133a to your computer and use it in GitHub Desktop.
email/sending_email_with_gmail.ipynb
This file contains 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
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import smtplib\nfrom email.mime.text import MIMEText\nfrom email.utils import COMMASPACE\nfrom getpass import getpass", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "OUTLOOK_USER = input(\"Enter your Outlook email: \")\nOUTLOOK_PWD = getpass(\"Enter password: \")", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Enter your Outlook email: john_doe@somecompany.com\nEnter password: ········\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"collapsed": true, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "SUBJECT = 'Test Email'\nBODY = 'Just a Test'\nTO = 'recipient_email.com'\n\ndef sendEmail(sender, pwd, to, subject, message):\n recipient = to if type(to) is list else [to]\n msg = MIMEText(message)\n msg['Subject'] = subject\n msg['From'] = sender\n msg['To'] = COMMASPACE.join(recipient)\n server = smtplib.SMTP('smtp.office365.com:587')\n server.ehlo()\n server.starttls()\n\n try:\n server.login(sender,pwd)\n print('Successfully authenticated...')\n except smtplib.SMTPAuthenticationError: # Check for authentication error\n return \" Authentication ERROR\"\n\n try:\n server.sendmail(sender,recipient,msg.as_string())\n print('Email sent!')\n except smtplib.SMTPRecipientsRefused: # Check if recipient's email was accepted by the server\n return \"ERROR\"\n server.quit()", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "sendEmail(OUTLOOK_USER, OUTLOOK_PWD, TO, SUBJECT, BODY)", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Successfully authenticated...\nEmail sent!\n" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/e3f17a7df67d6ec7ee350186d105d839" | |
}, | |
"gist": { | |
"id": "e3f17a7df67d6ec7ee350186d105d839", | |
"data": { | |
"description": "email/sending_email_with_gmail.ipynb", | |
"public": true | |
} | |
}, | |
"hide_input": false, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.3", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment