Loading video player…

Sending Email With Python

00:00 In the previous lesson, I showed you how the text-based SMTP gets used to send an email. In this lesson, I’ll show you what you came for, how to use this from Python.

00:10 Saying hello is friendly and all, but a bit of a pain. So instead, Python has a module that does all that for you, smtplib. The module provides a class that you can use to connect to an SMTP server, and then methods on that class for sending a message. For more complex messages, there’s a lot of stuff you have to fill in. So to help with that, Python’s email module has the EmailMessage class.

00:35 This is a dictionary-like object you can use to build email messages. You can use it to specify who you are, who is getting the message, what goes in the body, and more.

00:45 Then once you’ve got your EmailMessage object built, you pass it to the SMTP object’s send_message() method, and it connects to the server and sends the mail for you.

00:56 Let’s go write some code that emails Alice. In the top window here, I have our first program, simple_send.py. Let me walk you through it. To start out, I need to import smtplib, which has the class for sending, and the EmailMessage class that I mentioned that helps you build a message.

01:14 There are ways of sending an email with just smtplib, but I find using the EmailMessage class is the easier route, so that’s what I’m going to show you how to do.

01:24 I’ve defined a few constants for later, the hostname and port that I’m connecting to. For our simple program, I could hardcode these values further down, but by doing it up here, it’s obvious what needs to change to point this at a different server.

01:38 Likewise, for who is sending and who is receiving this email. These will be the values that get used with the mail from and recipient to fields in the underlying protocol.

01:49 This line is where I’m instantiating the SMTP class from the smtplib library. I’m instantiating it as a context manager. When you’re done talking to an email server, you’re supposed to close the connection with the quit command. By instantiating this with a context manager, that gets done for you automatically when the with block completes.

02:10 The constructor takes two arguments, the HOST and PORT of the server to connect to. I’m using the constants I defined earlier for this, which you’ll recall are the HOST and PORT of my development server.

02:23 Inside of the with block, I start by building an email message object. Unfortunately, it doesn’t take arguments. It would be nice to just accept keyword args like the dictionary it’s built on, but for some reason they didn’t make it that way. Once I’ve got the object, I set the from key to my sender value, the to key to the receiver value, the subject key to populate the subject header, then call set_content() to pass in the message body.

02:51 You’ll recall from the previous lesson, this is all you need to do to send something. And that’s what this line does. It sends the message. Let me start a server and try this out.

03:05 Note, I’m not using the -d flag this time, so there’s no reply back to tell you it’s running. It just sits there. Now I’ll call the program.

03:18 And I get the message on the bottom. It went by kind of fast, so let me scroll this up.

03:25 You’ll notice there are a few more headers here than when I did this manually. By setting the to and from keys in the email message object, the mail from and recipient pieces get set, and headers for those corresponding emails get set as well.

03:40 And then you get the subject, but there’s more after that as well. The Content-Type header says what is in the message, which in our case is plain old text using UTF-8 encoding.

03:51 These headers are optional, but make it easier for the email client to display things the way you intended when you sent them.

03:58 MIME is a mechanism for specifying what kind of content is inside the message. The MIME-Version header indicates which kinds of content specifiers can be used.

04:08 The last header is the X-Peer that our dev server uses to indicate where the client connected from. And then of course below that is the body of the message.

04:18 Congrats, you’ve sent your first email with Python. Next up, I’ll get a little fancier, adding HTML formatting to the mix.

Become a Member to join the conversation.