1
00:00:01,340 --> 00:00:08,180
In the previous video, you learned how to send a plain text email using Outlook or Hotmail.

2
00:00:08,360 --> 00:00:15,380
In this video, you'll learn how to sense a more rich email with HTML, where you can change the font

3
00:00:15,860 --> 00:00:17,600
by using HTML code.

4
00:00:19,130 --> 00:00:23,600
So, the code you see is the code that we wrote in the previous video.

5
00:00:23,840 --> 00:00:26,390
This code sends a plain text email.

6
00:00:26,660 --> 00:00:33,200
No, let's change what we need to change so that we can send HTML instead of text.

7
00:00:33,590 --> 00:00:39,170
So HTML, as you know, is the code that we use to build websites.

8
00:00:40,070 --> 00:00:44,510
And so using that code, you can change the font and the size of the text.

9
00:00:45,560 --> 00:00:49,490
So to use the HTML in the body of the email

10
00:00:50,210 --> 00:01:00,440
we need to import a few classes from email.mime.text import

11
00:01:01,950 --> 00:01:04,160
MiMETexts.

12
00:01:05,069 --> 00:01:16,350
So with capital letters up to here and then ext, we also need from email.mime.multipart

13
00:01:17,040 --> 00:01:17,790
import

14
00:01:19,090 --> 00:01:26,110
MIMEMultipart, you're going to see what exactly these 
classes are used for.

15
00:01:27,250 --> 00:01:34,360
So let's leave those imports there for now, and then we declare the sender, which should be Hotmail

16
00:01:34,360 --> 00:01:35,140
or outlook.

17
00:01:35,590 --> 00:01:42,130
Again, the receiver could be any email address and the password is the password of the sender.

18
00:01:43,360 --> 00:01:50,650
Then, instead of creating one single variable which has this multiline string, we want to delete

19
00:01:50,650 --> 00:01:51,010
that.

20
00:01:51,460 --> 00:01:54,100
And this time we want to create

21
00:01:55,720 --> 00:02:01,960
a MIMEMultipart class instead of just a plain string.

22
00:02:02,650 --> 00:02:05,080
So what is MIMEMultipart?

23
00:02:05,320 --> 00:02:15,180
Well, mime means multi-purpose internet mail extensions, which is a standard that allows you to

24
00:02:15,190 --> 00:02:21,070
send not only text, but all the characters and attachments through emails.

25
00:02:22,240 --> 00:02:27,040
So with this, we are sending an email with different parts, so to say.

26
00:02:27,520 --> 00:02:33,570
And in here, we simply instantiate MIMEMultipart and then

27
00:02:33,580 --> 00:02:41,530
we refer to that variable message, and then we refer to keys of that message.

28
00:02:41,560 --> 00:02:47,750
So this is in the form of a dictionary, basically, and it's has different keys.

29
00:02:47,770 --> 00:02:57,880
So from with a Capital F, this defines who the sender is, so message from equal to sender and then

30
00:02:58,690 --> 00:02:59,290
message

31
00:03:01,190 --> 00:03:01,760
to.

32
00:03:04,320 --> 00:03:10,830
So we are basically assigning values to the two key and to the from key of the message dictionary.

33
00:03:11,640 --> 00:03:17,790
This is the receiver, of course, and message subject.

34
00:03:17,880 --> 00:03:26,310
Now the subject is always plain text so we can use a string here such as hello again.

35
00:03:27,710 --> 00:03:35,990
Now, the body of the email is a bit not as simple as declaring like that, like we did from

36
00:03:36,140 --> 00:03:44,480
to and subject for the body, you first want to create again, a multi-line string with triple quotes.

37
00:03:44,780 --> 00:03:48,500
But now this can be HTML code.

38
00:03:48,770 --> 00:03:53,660
So for example, if you use an h1 tag and say "Hi there".

39
00:03:54,960 --> 00:04:04,200
And then you close the H1 tag with those characters,  so "Hi there" will be displayed as big text

40
00:04:04,500 --> 00:04:06,590
because it has this heading.

41
00:04:07,990 --> 00:04:09,610
A big heading h1.

42
00:04:09,880 --> 00:04:12,700
If you want it smaller, you'd want to use a h2.

43
00:04:13,270 --> 00:04:17,980
So let's keep it h2, and then you can write other text.

44
00:04:20,060 --> 00:04:28,640
H3 or just plain text, such as there are only two cats flying today.

45
00:04:30,200 --> 00:04:32,090
Let's hope for more.

46
00:04:32,870 --> 00:04:37,790
So that's the body of the email and you'll see how it looks like.

47
00:04:37,790 --> 00:04:44,710
Now once you create the body as a string, you want to attach it somehow to the message variable. To do that

48
00:04:46,340 --> 00:04:51,840
you use the attach method, so we're not doing any email attachments.

49
00:04:51,920 --> 00:04:59,060
Don't confuse it with the attachment, this is just a method that attaches the body of the email.

50
00:04:59,450 --> 00:05:02,180
This is variable as the email body.

51
00:05:02,870 --> 00:05:13,070
Now this attach method expects not this body variable, but a more specialized object, and that is.

52
00:05:16,100 --> 00:05:18,960
Let's say this variable is

53
00:05:18,980 --> 00:05:27,320
MIMEText, so that class that we imported there, which gets as inputs, the string, which contains

54
00:05:27,320 --> 00:05:30,170
the body of the email, so body.

55
00:05:30,170 --> 00:05:35,930
But also you want to declare if this is plain text or HTML.

56
00:05:36,590 --> 00:05:44,840
If you leave it as plain, then these h two tags they will be displayed as they are in the email that

57
00:05:44,840 --> 00:05:48,680
will be received by the receiver, so they will see these tags.

58
00:05:48,680 --> 00:05:55,430
If you say HTML, then Python will understand that this is HTML and it will display this

59
00:05:55,430 --> 00:05:56,330
in big size.

60
00:05:56,540 --> 00:05:58,880
This "Hi there" without these tags.

61
00:06:00,590 --> 00:06:08,960
So that's what we want to do, and then mimetext goes in here so that it goes in here and then we establish

62
00:06:08,960 --> 00:06:10,850
the server connection.

63
00:06:11,540 --> 00:06:14,350
We start with TLS protocol.

64
00:06:14,360 --> 00:06:22,780
We log in and then we send an email from the sender to the receiver and message.

65
00:06:22,790 --> 00:06:32,000
No message is not the appropriate object type here because this 'sendemail', expects a string.

66
00:06:32,300 --> 00:06:33,400
But the message is not.

67
00:06:33,410 --> 00:06:35,780
A string is some sort of dictionary.

68
00:06:36,740 --> 00:06:39,260
You see, it has these different keys and values.

69
00:06:39,740 --> 00:06:48,620
So what we want to do here is we want to convert that dictionary type of object to a string.

70
00:06:48,890 --> 00:06:53,870
To do that, you want to use message that as string.

71
00:06:55,070 --> 00:07:01,730
So that methods and store that into a variable message text, for example.

72
00:07:02,450 --> 00:07:09,560
And I'm just going to print it out for you to see what this is about and then provides here message

73
00:07:09,560 --> 00:07:12,910
text instead of message and quit the server.

74
00:07:12,920 --> 00:07:13,690
And that's it.

75
00:07:13,880 --> 00:07:20,810
I'm going to run this now, and I will be expecting an email to the receiver email address from my

76
00:07:20,810 --> 00:07:22,250
Hotmail account.

77
00:07:22,790 --> 00:07:24,680
So I pressed on send.

78
00:07:27,250 --> 00:07:33,550
And we get this output. The first part is the output of the print function here.

79
00:07:33,760 --> 00:07:35,230
So that's the message text.

80
00:07:37,860 --> 00:07:39,000
It includes

81
00:07:40,640 --> 00:07:44,480
all that, the content type, these protocols.

82
00:07:44,510 --> 00:07:46,850
And then here again.

83
00:07:46,940 --> 00:07:51,080
So it's HTML and that's the body of the email.

84
00:07:52,690 --> 00:07:56,320
And then we have this error, probably you didn't receive it . If you did,

85
00:07:56,650 --> 00:08:01,390
that means your Hotmail or Outlook account is not verified.

86
00:08:01,750 --> 00:08:09,010
So if you receive this error, you should have received an email to your Hotmail or Outlook.com.

87
00:08:09,580 --> 00:08:14,390
That email is asking you to verify your email address using a phone number.

88
00:08:14,410 --> 00:08:15,580
So you have to do that.

89
00:08:16,270 --> 00:08:22,600
So I'm going to read that email and verify my account and then come back here and run this code again.

90
00:08:25,130 --> 00:08:29,270
OK, I have verified my Hotmail account, so I can run the code again.

91
00:08:31,990 --> 00:08:33,370
No errors this time.

92
00:08:34,059 --> 00:08:37,990
So let's check if we got the email.

93
00:08:39,309 --> 00:08:41,440
Check in your spam folder as well.

94
00:08:43,010 --> 00:08:44,750
Or wait a while.

95
00:08:47,530 --> 00:08:53,680
And yeah, here is a new email from automateeverythingwithpython@hotmail.com.

96
00:08:54,070 --> 00:08:59,380
So that's the email body. "Hi there" is .

97
00:08:59,500 --> 00:09:00,440
So it's a heading.

98
00:09:00,880 --> 00:09:03,850
It's in big fonts and that's the subject also.

99
00:09:05,220 --> 00:09:11,610
So that's a complete code on how to send emails using HTML in the body of the email.

100
00:09:12,180 --> 00:09:19,230
In the next video, I'll show you how to attach a file to an email that you send with your outlook or

101
00:09:19,230 --> 00:09:20,070
Hotmail account.

102
00:09:20,340 --> 00:09:21,570
So I'll see you in the next video.

