Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Connecting to the Mail Server With smtplib

Give Feedback

In this video, you’ll learn how to set up a basic connection to a SMTP server using Python’s smtplib and ssl modules. Additionally, a prospect of the following video is given.

00:00 Now that you have your email account configured, you need to learn how to connect to the mail server securely using smtplib. You’ll also learn how to use the ssl library to do some heavy lifting on the encryption side.

00:12 SSL, or Secure Sockets Layer, and TLS, or Transport Layer Security, are two protocols for encrypting connections between machines. I’ll be using the two terms interchangeably for the rest of these videos, as Gmail is going to upgrade an SSL connection to TLS, which is a more recent and secure standard.

00:32 If the differences between these two protocols is confusing, don’t worry. We don’t need to understand the difference between the two, just that there is a difference between an encrypted and an unencrypted connection. First, let’s establish a secure connection from the start using the SMTP_SSL() method. In your editor, go ahead and import smtplib

00:56 and ssl.

00:59 Next, define some parameters. So in the case of Gmail, this is going to be 'smtp.gmail.com' and we’re going to access port 465 for SSL. Add a variable for your email address, which you can call sender. So in my case, this is 'thecatinthehacks@gmail.com'.

01:29 And rather than saving a password, go ahead and just use an input() function,

01:37 'Enter your password here: '. Okay. Now to get your encryption context, create a variable called context and set this equal to ssl.create_default_context(), just like that.

01:55 And now when you’re connecting to the SMTP server, you can think of this as opening a file where you want to make a connection, do something with that connection, and then close the connection.

02:06 You can use a with statement, so with smtplib.SMTP_SSL(). Pass in the parameters you defined before, so smtp_server, port,

02:23 and set the context equal to context, and let’s go ahead and call this server.

02:31 I’m actually going to close out the project window over here, get some more space. Now, go ahead, and with that server log in using the sender email and the password.

02:46 Now once this is logged in, it’ll do the login and then nothing else will happen. So to make sure that we see that, just add a print() statement here that says something like 'It worked!', just to make sure that the connection works before you actually send an email.

03:04 So, let’s try it out! Go ahead, open up a terminal, and run the script.

03:13 It really helps when you save the scripts before you try to run them, so let’s open that back up and try it again. Okay. It says to enter a password.

03:28 I’m going to put my password in. And there we go! It shows that It worked!. Just to make sure that this connection is actually working and we’re not just printing out that statement, let’s go ahead and try to put in the wrong password. So run the script again, put in an incorrect password, and you get this error message.

03:49 Let’s take a look at it. Looking down here, there’s this SMTPAuthenticationError […] Username and Password not accepted. So the connection looks like it’s working. And that’s all there is connecting to a mail server securely from the start. In the next video, you’ll learn how to use the .starttls() method, which creates opportunistic encryption where you create an unencrypted connection and then later upgrade it to an encrypted connection when you need to. Thanks for watching.

Pygator on Aug. 25, 2019

how did you blur your password, when you input it for the script?

Joe Tatusko RP Team on Aug. 27, 2019

Hi Pygator! I’m actually doing that in my screen recording software. Python has libraries available if you want to have a traditional input field that doesn’t show the password, but I felt like they were outside the scope of this course. If you get a chance check out the getpass module!

Pygator on Aug. 31, 2019

Thanks for the module recommendation. Just finished the course.

vishukamble on Oct. 8, 2019

Pygator, You can use getpass module. From getpass import getpass password = getpass()

This will ask for password in the same way, it just won’t display the characters like in a linux terminal.

Aaron Horne on March 14, 2020

Not working for me.

Even installed secure-smtplib 0.1.1 even though some say smtplib is installed by default with python.

The error I get is:

Traceback (most recent call last):
  File "email.py", line 1, in <module>
    import smtplib, ssl
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 47, in <module>
    import email.utils
  File "/Users/ahorne/Google Drive/scripts/email.py", line 13, in <module>
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
AttributeError: module 'smtplib' has no attribute 'SMTP_SSL'

gracetan on April 23, 2020

Hi Joe, i am wondering why do i have to put context=context after smtplib.SMTP_SSL? I tried to remove it, and the prorgam still worked. By reading the code requirment for the SMTP_SSL, it seems that context is optional.

Become a Member to join the conversation.