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