In the previous video, you learned how to send a plain text email using Outlook or Hotmail.
In this video, you'll learn how to sense a more rich email with HTML, where you can change the font
by using HTML code.
So, the code you see is the code that we wrote in the previous video.
This code sends a plain text email.
No, let's change what we need to change so that we can send HTML instead of text.
So HTML, as you know, is the code that we use to build websites.
And so using that code, you can change the font and the size of the text.
So to use the HTML in the body of the email
we need to import a few classes from email.mime.text import
MiMETexts.
So with capital letters up to here and then ext, we also need from email.mime.multipart
import
MIMEMultipart, you're going to see what exactly these classes are used for.
So let's leave those imports there for now, and then we declare the sender, which should be Hotmail
or outlook.
Again, the receiver could be any email address and the password is the password of the sender.
Then, instead of creating one single variable which has this multiline string, we want to delete
that.
And this time we want to create
a MIMEMultipart class instead of just a plain string.
So what is MIMEMultipart?
Well, mime means multi-purpose internet mail extensions, which is a standard that allows you to
send not only text, but all the characters and attachments through emails.
So with this, we are sending an email with different parts, so to say.
And in here, we simply instantiate MIMEMultipart and then
we refer to that variable message, and then we refer to keys of that message.
So this is in the form of a dictionary, basically, and it's has different keys.
So from with a Capital F, this defines who the sender is, so message from equal to sender and then
message
to.
So we are basically assigning values to the two key and to the from key of the message dictionary.
This is the receiver, of course, and message subject.
Now the subject is always plain text so we can use a string here such as hello again.
Now, the body of the email is a bit not as simple as declaring like that, like we did from
to and subject for the body, you first want to create again, a multi-line string with triple quotes.
But now this can be HTML code.
So for example, if you use an h1 tag and say "Hi there".
And then you close the H1 tag with those characters, so "Hi there" will be displayed as big text
because it has this heading.
A big heading h1.
If you want it smaller, you'd want to use a h2.
So let's keep it h2, and then you can write other text.
H3 or just plain text, such as there are only two cats flying today.
Let's hope for more.
So that's the body of the email and you'll see how it looks like.
Now once you create the body as a string, you want to attach it somehow to the message variable. To do that
you use the attach method, so we're not doing any email attachments.
Don't confuse it with the attachment, this is just a method that attaches the body of the email.
This is variable as the email body.
Now this attach method expects not this body variable, but a more specialized object, and that is.
Let's say this variable is
MIMEText, so that class that we imported there, which gets as inputs, the string, which contains
the body of the email, so body.
But also you want to declare if this is plain text or HTML.
If you leave it as plain, then these h two tags they will be displayed as they are in the email that
will be received by the receiver, so they will see these tags.
If you say HTML, then Python will understand that this is HTML and it will display this
in big size.
This "Hi there" without these tags.
So that's what we want to do, and then mimetext goes in here so that it goes in here and then we establish
the server connection.
We start with TLS protocol.
We log in and then we send an email from the sender to the receiver and message.
No message is not the appropriate object type here because this 'sendemail', expects a string.
But the message is not.
A string is some sort of dictionary.
You see, it has these different keys and values.
So what we want to do here is we want to convert that dictionary type of object to a string.
To do that, you want to use message that as string.
So that methods and store that into a variable message text, for example.
And I'm just going to print it out for you to see what this is about and then provides here message
text instead of message and quit the server.
And that's it.
I'm going to run this now, and I will be expecting an email to the receiver email address from my
Hotmail account.
So I pressed on send.
And we get this output. The first part is the output of the print function here.
So that's the message text.
It includes
all that, the content type, these protocols.
And then here again.
So it's HTML and that's the body of the email.
And then we have this error, probably you didn't receive it . If you did,
that means your Hotmail or Outlook account is not verified.
So if you receive this error, you should have received an email to your Hotmail or Outlook.com.
That email is asking you to verify your email address using a phone number.
So you have to do that.
So I'm going to read that email and verify my account and then come back here and run this code again.
OK, I have verified my Hotmail account, so I can run the code again.
No errors this time.
So let's check if we got the email.
Check in your spam folder as well.
Or wait a while.
And yeah, here is a new email from automateeverythingwithpython@hotmail.com.
So that's the email body. "Hi there" is .
So it's a heading.
It's in big fonts and that's the subject also.
So that's a complete code on how to send emails using HTML in the body of the email.
In the next video, I'll show you how to attach a file to an email that you send with your outlook or
Hotmail account.
So I'll see you in the next video.