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