Emails with Python
Complete Python Bootcamp
● In this section we will explore how to send
emails with Python and how to check our
inbox for received messages.
● Please keep in mind this process is highly
reliant on admin privileges on both your
local computer, your internet, and your
email.
Complete Python Bootcamp
● It is highly likely that on a corporate
network, work computer, or work email
these methods will be blocked for security
reasons.
● If you encounter issues due to this, please
contact your IT department, as it is not an
issue we can fix on our end.
Complete Python Bootcamp
● Lastly, there is no full exercise for these
lecture topics, since there is no real way
we can create true self-assessments for a
personal email address.
● However, we do provide a notebook with
some ideas for you to explore, we
encourage you to get creative!
Complete Python Bootcamp
Let’s get started!
Sending Emails
Complete Python Bootcamp
● To send emails with Python, we need to
manually go through the steps of
connecting to an email server, confirming
connection, setting a protocol, logging on,
and sending the message.
Complete Python Bootcamp
● Fortunately the built-in smtplib library in
Python makes these steps simple function
calls.
Complete Python Bootcamp
● Each major email provider has their own
SMTP (Simple Mail Transfer Protocol)
Server.
Complete Python Bootcamp
● We will go over this process with a Gmail
account.
● For gmail users, you will need to generate
an app password instead of your normal
password.
● This let’s Gmail know that the Python
script attempting to access your account is
authorized by you.
Complete Python Bootcamp
● Let’s explore this entire process.
Receiving Emails
Complete Python Bootcamp
● To view received emails with Python we
can use the built in imaplib and email
libraries in Python.
● The imaplib library has a special syntax for
searching your Inbox.
Complete Python Bootcamp
Complete Python Bootcamp
● Before beginning this discussion, send
yourself a test email with a unique subject
line that you will be able to remember and
search for using Python.
● Let’s begin exploring checking received
emails with Python.