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