#100DaysOfCode in Python Transcripts
Chapter: Days 64-66: Sending emails with smtplib
Lecture: Sending an Email with smtplib

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, let's look at that blank slate, it's a bit daunting, isn't it? So let's fill it up.
0:07 Now, this one here is going to have a few different sections, okay? It might be a bit complex if you're quite new to this stuff
0:15 but just roll with it and let's see how we do, okay? So we're going to start off our application using Python 3 and we're going to import our smtplib.
0:27 Nice, all right. So when we send an email using smtplib, we need an address that we're sending from. That's going to be our Gmail, right?
0:39 And we need to send the email to someone, so let's just set up two variables for ourselves, okay? from_address, let's use the PyBites email.
0:53 Feel free to spam, no actually don't spam us on that, please and you can send us some fan mail,
1:00 maybe some, "Hey guys, you're doing a great job," emails. Or Julien is cooler than Bob. No, I'm just kidding. So we have to_address,
1:09 well, we're going to send it to ourselves, okay? That way I don't accidentally spam someone 'cause that actually did happen.
1:16 Now, the email needs body, right? This is the text that's going to be populating our email script, or our email that we send out, okay?
1:27 Now, I use this script for sending out my email to myself for new releases on Steam, you know, the game platform.
1:37 So this just going to be a string of some sort, okay? It could be a paragraph, it could be whatever you want.
1:44 However it shows up here is going to be the plain text that shows up in your email, okay? That includes carriage returns and whatnot,
1:52 so we'll just go new releases on and sales on Steam. Click the links below to check them out. Excellent, done. Get that right.
2:09 Now, for the nitty-gritty, all right? So the first thing we need to tell our script is what smtp server we're using.
2:18 So while we in our heads are thinking yes, let's use some Gmail, our script actually has no idea. So we're going to set up a little...
2:26 Oops, smtp, not pt, I always get that wrong server equals and now we start using our... Oh jeez, every time. Now we start using our smtplib module so
2:40 smtp and within the brackets, this is where we want to use the Gmail settings. Now, these just copy off me smtp.gmail.com.
2:51 You can easily google for these, okay? If you just search for Gmail smtp settings, you'll get them, okay? And the port number to use is 587.
3:02 That's just a number, you don't need to put the apostrophes around it, the quotes around it, okay?
3:09 Now, one cool thing that smtplib does is it essentially... It requires you to send a hello message, almost think of it like a heartbeat, right?
3:19 It has to send this hello message to the smtp server. And that way, if there is a failure, if, for some reason, the server is unreachable,
3:32 you will get an error in return and your script won't run, it won't go through all of these steps for nothing, okay?
3:40 And we do that using smtpserver.ehlo, that's it, okay? By calling that, by running that, we send the sort of heartbeat off to the smtp server
3:54 going, "Are you there? Can you respond?" That sort of thing, all right? Next up, we want to start the encryption, okay?
4:02 We're using TLS because it's Gmail. This is all you googly available, all right? Google it, you'll find out, okay? So start TLS, that's it.
4:16 Start our encrypted session, right? And now, we do the login. So makes a bit of sense, right? We want to start an encrypted line first
4:25 before we put in any sort of cryptic details, okay? So smtp, now we want to actually provide our details to the server so we'll logging in, all right?
4:37 So smtpserver.login and we're going to put our email that we're sending the email from, okay? This is your Gmail account that you'll be using
4:50 for automation that you're sending the emails from. And now, in this section here, you put your application ID, password,
5:02 whatever you want to call it, okay? Now, I'm obviously not putting mine in there because I don't want you to use my email to spam me
5:11 or get up to other sort of mischief, right? Now that that's there, we can go smtpserver.sendmail. Yay, send mail, this is the actual fun part,
5:23 this is where we're sending our email. So we need three things here. What do we need? We need the from_address,
5:29 we need to know where we're sending the email from. We need to know where we're sending the email to
5:34 and we need the stuff that populates the email, all right? So we have from_address, we have to_address and then we have the body.
5:46 There we go, from_address, to_address and body. That's it. That's all, we're done. Now, as we've come to learn a lot of modules require us
5:58 to close the connection, okay? So we're going to close our connection to this and smtp server and I like to add something just for login,
6:09 email sent successfully and that is that. So if you've done all of that right, and you run this script, you will end up with an email, okay?
6:22 And it will just be a nice, simple plain text email, you'll notice a few things about it which I'll show you in just a second.
6:28 Obviously, I can't run this one, so I actually have this script fully written with my application ID elsewhere, here's one I prepared earlier.
6:36 And this is what the email looks like. And we just bring up Gmail here, and there we go. So you can see there's an email that was sent,
6:47 it says new releases and sales on Steam, click the links below to check them out. That's it, right? We only specified


Talk Python's Mastodon Michael Kennedy's Mastodon