#100DaysOfCode in Python Transcripts
Chapter: Days 64-66: Sending emails with smtplib
Lecture: Concepts: what did we learn

Login or purchase this course to watch this video and the rest of the course contents.
0:00 How easy was that? This is pretty much one of the coolest scripts because you almost just have to write it once. And then you can copy and paste it
0:09 for whatever project you want, obviously remembering a couple of good practices there which is one of them being to create
0:16 your own application id for every application, right? Okay, so, what did we do? What's our recap? Well, we got our Gmail application password.
0:27 There's your quick guide on how to do it. Again, a quick Google will get you everything you need to find out how to do that,
0:36 but I will provide the links. Next, send email with smtplib. Well, we began by importing smtplib, go figure. Right, then, we specified the server
0:51 that we're running, specifically the Gmail server. Then, we checked for a heartbeat, using that hello message, okay,
1:00 that's just to make sure the server's okay and ready to go. And then we started the TLS encryption, all right? Next, we sent through our login details,
1:11 and just a word of advice, never, ever, ever hardcode your application password into your script. Okay, never do that.
1:20 The safest thing you can probably do that's also super simple, is make your application password an environment variable, okay?
1:30 And then use something like os, or whatever to, reference that id, okay, in variable form. Import it, reference it, okay?
1:44 Next, we sent the email from address to address body with a sendmail and then we quit. We closed that connection to the SMTP server.
1:54 And we got a nice, plain-text email. Next, we have send an email with MIME. Okay. This one was a little more complex.
2:06 We started by importing the required modules, right, Multipart and text. Then, we created the Multipart object in Message.
2:15 Again, that's just nice and easy. All right, we built the header. From, to, and subject, yes? Yes, yes, we all got that.
2:26 That was so that we had all that extra information, made our emails a little more functional and beautiful.
2:33 All right, now, we had to attach our body text to this Message object, okay? Remember, when you specify your body text
2:43 or whatever's going to be in the email, you don't do it using Message, you just sort of create your variable and then you attach it, all right?
2:50 That's what MIME text is for. And then, we took that entire awesome Message, Body, Header object that we'd created and we turned it into a nice string,
3:04 and we assigned that to text, and that way, sendmail, beautiful sendmail could talk to it and send the email off, all right?
3:13 And I've also included this little BCC trick in there. Remember, we took our to_address and made it a list,
3:20 that's this here, and then we had our existing list of BCC emails, whether we pulled that in from external or, I guess, hardcoded it into the script,
3:31 whatever floats your boat, and we combined the two lists and sent them out. And that is it, my friends. It is your turn.
3:40 Now, I reckon, really cool thing you can try is to write this script yourself. As I hinted at in one of the videos,
3:49 find something that you can populate into your email with some sort of Python data structure or process, whether it be just a list of names,
4:00 it could be something from a database, whatever, try your hand at that, and if you smash that and you have some extra time,
4:07 I reckon try and automate it. Set yourself up a cron job if you're running Linux, hopefully, or a scheduler on Windows,
4:14 whatever you can find, whatever you have, just Google around, find a way to automate it, and maybe send your mates some annoying emails,
4:23 maybe just your smiley face with a thumbs-up. Do something like that. In fact, I might go and do that to Bob now. So enjoy, keep calm, and code.


Talk Python's Mastodon Michael Kennedy's Mastodon