#100DaysOfCode in Python Transcripts
Chapter: Days 64-66: Sending emails with smtplib
Lecture: Getting into MIME

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we get started with the actual code,
0:03 let's just understand a bit what MIME is.
0:06 MIME is actually an acronym for
0:09 Multipurpose Internet Mail Extensions.
0:13 I'd rather say MIME than all of that.
0:16 With MIME, it actually extends email functionality.
0:21 You think about all the cool HTML stuff,
0:24 and audio, video, images and so on
0:27 that you can attach to emails, and all of that.
0:30 That's all thanks to MIME.
0:33 It's not plain text.
0:35 It actually allows you to give your emails multiple parts
0:41 such as header information, and all of those nice things.
0:44 One thing you'll notice looking at the screen here
0:46 is without email from the previous video,
0:49 the SMTPlib sendmail, we don't have a subject.
0:53 We don't really have much at all.
0:55 There was nothing for BCC.
0:58 There was nothing.
1:00 That was the real basic, basic stuff.
1:04 With MIME we get to go a bit further than that.
1:07 Let's hop into the emailer-mime.py file you created,
1:11 and let's get cracking.
1:13 Now we're import smtplib as we did before.
1:20 We'll keep that the same.
1:22 Now we need to actually start importing the MIME modules.
1:26 It's not too bad.
1:29 Just roll with it here, alright?
1:30 This seems a bit complex, but from email.MIME,
1:37 this is all just the stuff in the module
1:39 that we're taking out, okay?
1:42 We're not going to import everything, just what we need.
1:45 From email.mime.multipart, import MIMEMultipart.
1:51 This is the module that's going to allow us to,
1:55 I guess, section up our email.
1:58 Build our email together, alright?
2:01 You'll see what that means in a minute so just roll with it.
2:04 from email.mime.text, import MIMEText.
2:11 This is just to do with the text section.
2:14 Again, you'll see how this all fits together in a minute.
2:18 Alright, so we'll stick with what we know.
2:20 We get a from_address.
2:22 pybitesblog@gmail.com, okay?
2:26 Now we want a to_address.
2:29 Now one thing I would like you to consider here
2:33 is the BCC, the essence of BCC, alright?
2:38 That was a carbon copy, blind carbon copy.
2:42 That means no one can see who's been BCC'd on an email.
2:47 Now with that, you'll notice that MIME actually fails us.
2:51 I'm going to touch on that in the next video,
2:54 so don't panic.
2:55 Let's just go with this, okay?
2:58 To address, let's again stick with what we know.
3:04 pybytesblog@gmail.com.
3:07 Alright, now what we want to do is we want to take
3:13 the ability to build our email using multipart.
3:19 We're going to take that function and we're going to assign it
3:23 to the message object, alright?
3:25 We're going to make that our message object.
3:29 Why do we do that?
3:30 Just because it's easier to use message
3:32 instead of my multipart.
3:34 Again, you will see what I mean.
3:37 Alright, so what are the different sections
3:38 that we want to build?
3:39 Well we want to build our header.
3:41 To build our header, this is the format.
3:44 We want to have a from field, so message from...
3:51 Is what?
3:52 Well it's going to be our from_address, that's it.
3:57 What we've done is we've actually built
3:58 this little header tag that will actually extend
4:01 the functionality of our email.
4:03 Our email will look nicer and will have that as a valid id
4:08 for the from field.
4:10 Next, we want to have to, so message to...
4:16 We're going to use our to address.
4:19 Nice and easy so far, right?
4:22 Now for the fun part.
4:24 Now we get to specify a subject.
4:27 Message subject equals, well how about we take
4:31 that first line from the last one?
4:33 New releases and sales on steam, okay?
4:40 You can call that obviously whatever you want
4:41 for this practice round.
4:43 Now back to this, we'll build our body.
4:47 I'm just going to take the exact same text
4:51 from our previous script, dump it in there, right?
4:57 Now we want to build it all together.
5:00 We go message.attach, and now we're going to attach,
5:06 if you just paid attention for a second there
5:09 you would've seen that we just created body.
5:11 We didn't actually assign it to message.
5:15 This isn't included in our message.
5:18 That's what this attach thing is doing.
5:19 It's attaching our bulk, our body to the message.
5:24 Message.attach, and this is where MIMEText comes in.
5:30 Now we're taking our body.
5:32 Again, that's this object here, this variable.
5:35 What kind of an email, what kind of a body
5:39 is this going to be?
5:40 Well it's just going to be plain text, not okay text.
5:44 Yes, to answer your question, you can put HTML there.
5:48 Your body can be filled with HTML tags.
5:52 You can literally write HTML between these three here,
5:56 and it will form your HTML email.
5:59 That's just a bit beyond the scope of this
6:01 as it's a bit too much.
6:02 Alright, now we move into our normal SMTP stuff,
6:07 so a bit of magic.
6:09 We're just going to make all of that appear here,
6:12 and we're back.
6:13 It's exactly the same smtplib stuff
6:18 that we saw in the previous video.
6:20 Just chuck it in there.
6:21 You can feel free to edit your existing script,
6:23 whatever you want to do.
6:25 Now when we run sendmail, if you remember
6:29 we threw the body in there, okay?
6:32 We had the from_address, the to_address, and the body.
6:36 In this case, body has already been thrown into our message,
6:41 so how do we combine that?
6:42 How do we get this working?
6:43 Well sendmail needs string, it needs text.
6:49 It doesn't like an object like the message object
6:54 being thrown in there, okay?
6:56 What we need to do is get our message as a string.
7:03 message.as_string, and we'll assign that to text.
7:10 Again, this here text, you can make that whatever you want.
7:13 Let's give ourselves some white space.
7:15 Delete that in a minute.
7:17 Alright, and then we get back down to smtpserver.sendmail,
7:22 and we're going to go from_address, oops.
7:26 We're going to go to_address, and we're going to go text.
7:32 Then as usual, SMTPserver.quit, and we're done, okay?
7:40 Now let's just quickly throw in this
7:43 print email sent successfully.
7:46 Save that.
7:48 Now when you run your script,
7:50 you should get an almost identical email.
7:54 The body should be exactly the same, because again,
7:57 we didn't do anything differently here.
7:59 The thing that's cool is that you should have a subject,
8:02 and some more header information.
8:05 Let's have a look at that.
8:08 Alright, so there's the email.
8:10 New releases and sales on steam.
8:13 And now you can see the same information there.
8:16 When we drop that down we can see a to_address,
8:20 and which is my email,
8:22 and yeah, that's it.