{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", " \n", " \n", "
\n", "
\n", " \n", "
Content Copyright by Pierian Data and xDM Consulting
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Overview of Sending Emails" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The smtplib library allows you to manually go through the steps of creating and sending an email in Python:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import smtplib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an SMTP object for a server. Here are the main Server Domain Name for the top email services. If you don't see your email server here, you may need to do a quick Google Search to see if there SMTP server domain name is available:\n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "\n", " \n", " \n", "\n", "\n", "
ProviderSMTP server domain name
Gmail (will need App Password)smtp.gmail.com
Yahoo Mailsmtp.mail.yahoo.com
Outlook.com/Hotmail.comsmtp-mail.outlook.com
AT&Tsmpt.mail.att.net (Use port 465)
Verizonsmtp.verizon.net (Use port 465)
Comcastsmtp.comcast.net
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next is to create an STMP object that can make the method calls to log you in to your email in order to send messages. Notice how also specify a port number. If the number 587 does not work on your computer, try using 465 instead. Keep in mind, a firewall or antivirus may prevent Python from opening up this port, so you may need to disable it on your computer." ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "smtp_object = smtplib.SMTP('smtp.gmail.com',587)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we run the ehlo() command which \"greets\" the server and establishes the connection. This method call should be done directly after creating the object. Calling it after other methods may result in errors in connecting later on. The first item in the tuple that is returned should be 250, indicating a successful connection." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(250,\n", " b'smtp.gmail.com at your service, [47.143.81.4]\\nSIZE 35882577\\n8BITMIME\\nSTARTTLS\\nENHANCEDSTATUSCODES\\nPIPELINING\\nCHUNKING\\nSMTPUTF8')" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smtp_object.ehlo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When using the 587 port, this means you are using TLS encryption, which you need to initiate by running the starttls() command. If you are using port 465, this means you are using SSL and you can skip this step." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(220, b'2.0.0 Ready to start TLS')" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smtp_object.starttls()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now its time to set up the email and the passwords. You should never save the raw string of your password or email in a script, because anyone that sees this script will then be able to see you email and password! Instead you should use input() to get that information. If you also don't want your password to be visible when typing it in, you can use the built-in **getpass** library that will hide your password as you type it in, either with asterisks or by just keeping it invisible." ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# For hidden passwords\n", "import getpass" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Type something here and it will be hidden: ········\n" ] } ], "source": [ "result = getpass.getpass(\"Type something here and it will be hidden: \")" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Just keep in mind that its still visible as an object internally:\n", "result" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your passwords\n" ] }, { "data": { "text/plain": [ "'s'" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Or just use input()\n", "input(\"Enter your password\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "____\n", "**Note for Gmail Users, you need to generate an app password instead of your normal email password. This also requires enabling 2-step authentication. Follow the instructions here to set-up 2-Step Factor Authentication as well as App Password Generation:https://support.google.com/accounts/answer/185833?hl=en/. Set-up 2 Factor Authentication, then create the App Password, choose Mail as the App and give it any name you want. This will output a 16 letter password for you. Pass in this password as your login password for the smtp.**\n", "____" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "email = getpass.getpass(\"Enter your email: \")\n", "password = getpass.getpass(\"Enter your password: \")\n", "smtp_object.login(email,password)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can send an email using the .sendmail() method." ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter your email: ········\n", "Enter the email of the recipient: ········\n", "Enter the subject line: This is a test\n", "Type out the message you want to send: Here is the message.\n" ] }, { "data": { "text/plain": [ "{}" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from_address = getpass.getpass(\"Enter your email: \")\n", "to_address = getpass.getpass(\"Enter the email of the recipient: \")\n", "subject = input(\"Enter the subject line: \")\n", "message = input(\"Type out the message you want to send: \")\n", "msg = \"Subject: \" + subject + '\\n' + message\n", "smtp_object.sendmail(from_address,to_address,msg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you get back an empty dictionary, then the sending was successful." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can then close your session with the .quit() method." ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(221, b'2.0.0 closing connection j1sm22376227pgq.33 - gsmtp')" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "smtp_object.quit()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we know how to send emails, its time to learn how to look through emails you've already recieved." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }