1
00:00:01,810 --> 00:00:09,940
In this lecture, you'll learn how to send an email from an outlook or a Hotmail address.

2
00:00:11,190 --> 00:00:16,940
For that, we're going to be using smtplib.

3
00:00:17,280 --> 00:00:21,750
This is a standard Python library, meaning you don't need to install it.

4
00:00:21,780 --> 00:00:24,150
This comes shipped with Python.

5
00:00:24,930 --> 00:00:32,400
Once you import the library, then you want to prepare the email, which means you want to define

6
00:00:32,400 --> 00:00:33,780
who the sender is.

7
00:00:34,140 --> 00:00:42,000
In my case, that is a Hotmail address, so you can either put a Hotmail address or an outlook address

8
00:00:42,000 --> 00:00:42,240
here.

9
00:00:46,880 --> 00:00:49,610
This is a throwaway account that I have created.

10
00:00:50,780 --> 00:00:52,010
It's a Hotmail address.

11
00:00:53,120 --> 00:01:01,250
And then the receiver, the receiver in my case, would be a Gmail account, but it could be any email

12
00:01:01,250 --> 00:01:01,700
address.

13
00:01:05,610 --> 00:01:08,910
And the passwords of the sender.

14
00:01:09,810 --> 00:01:13,500
So the password of your Hotmail or outlook account.

15
00:01:13,810 --> 00:01:17,010
Now you can either place the password in here.

16
00:01:17,640 --> 00:01:25,680
I've created a temporary passwords for this account, so you can either place the password in here as

17
00:01:25,680 --> 00:01:29,580
a plain string or if you want to be more secure.

18
00:01:29,850 --> 00:01:31,050
So it's up to you.

19
00:01:31,060 --> 00:01:34,230
It is recommended to make it more secure.

20
00:01:34,620 --> 00:01:36,360
Like, I'll show you just now.

21
00:01:37,950 --> 00:01:38,640
But it's up to you.

22
00:01:38,670 --> 00:01:40,990
You can also leave it your passwords here.

23
00:01:41,700 --> 00:01:45,990
If that's not your main email account, for example.

24
00:01:46,320 --> 00:01:53,220
So if you want to be more secure and if you are using a Repl, then you can go to secrets.

25
00:01:53,230 --> 00:02:05,160
This icon here and then here add a key such as passwords, and the value. The value will be your actual

26
00:02:05,160 --> 00:02:05,730
passwords.

27
00:02:05,970 --> 00:02:07,080
So there.

28
00:02:08,120 --> 00:02:10,729
And Press on add new secret.

29
00:02:11,420 --> 00:02:20,630
So you have just created an environment variable in the Repl system, then you can delete that and

30
00:02:20,630 --> 00:02:28,250
use os.getenv and then place the name you gave to that environment variable.

31
00:02:28,670 --> 00:02:32,300
So passwords in capital letters, that's what I entered.

32
00:02:32,570 --> 00:02:34,850
And then import os.

33
00:02:35,030 --> 00:02:37,460
Now this is what you do in Repl.

34
00:02:38,870 --> 00:02:44,660
If you're using your local computer and you don't want to save the password as a plain python string

35
00:02:44,660 --> 00:02:44,960
here.

36
00:02:45,290 --> 00:02:47,360
Then you want to go to

37
00:02:47,360 --> 00:02:48,260
pythonhow.com.

38
00:02:49,600 --> 00:02:57,100
And then search for here in the box, search for passwords securely.

39
00:02:58,600 --> 00:03:09,850
And this is the article you should follow how to create a password securely on Windows and also on Mac

40
00:03:09,850 --> 00:03:10,570
or Linux.

41
00:03:10,900 --> 00:03:15,220
And then once you follow those instructions, then you write this line here.

42
00:03:15,580 --> 00:03:19,150
So the password will get the value from the environment variables.

43
00:03:19,780 --> 00:03:25,150
For simplicity, though, I'm just going to write python12345678.

44
00:03:25,480 --> 00:03:29,680
The plain password in here and then carry on with the message.

45
00:03:30,190 --> 00:03:36,310
So the message or the content would be a multi-line string.

46
00:03:37,090 --> 00:03:42,130
Now what we're sending this time in this video is of plain text email.

47
00:03:42,700 --> 00:03:46,590
So it's not an email that contains different fonts.

48
00:03:47,080 --> 00:03:53,170
For that, you'd need an HTML email, which is what we're going to do in the next video.

49
00:03:53,620 --> 00:03:55,180
So this is just plain text.

50
00:03:55,330 --> 00:03:57,280
No fonts, no attachments.

51
00:03:58,120 --> 00:04:08,530
And the syntax for this is you need here a back slash and then you write with a capital S subject, colon,

52
00:04:08,770 --> 00:04:12,850
space and then write the subject of the email.

53
00:04:13,420 --> 00:04:14,790
For example, hello, hello.

54
00:04:15,310 --> 00:04:24,350
Then you want to make a break line and leave an empty line there and then carry on with the body of

55
00:04:24,490 --> 00:04:24,990
email.

56
00:04:25,600 --> 00:04:26,620
So the content.

57
00:04:27,190 --> 00:04:28,290
This is Ardit.

58
00:04:30,700 --> 00:04:33,400
Just wanted to say hi.

59
00:04:34,360 --> 00:04:35,910
So you should follow.

60
00:04:35,920 --> 00:04:41,140
You must follow this syntax with triple quotes here.

61
00:04:41,140 --> 00:04:42,130
Triple quotes here.

62
00:04:42,250 --> 00:04:43,780
So it's a multiplying string.

63
00:04:44,320 --> 00:04:46,090
Then you have the backslash here.

64
00:04:46,990 --> 00:04:55,360
Subjects, column, space, of the subject and then a brake line, empty line and then the content.

65
00:04:55,900 --> 00:04:57,430
The body of the email.

66
00:04:59,870 --> 00:05:04,580
So that concludes the preparation of the email content.

67
00:05:04,850 --> 00:05:15,290
Now we want to start up the server, the SMTP server and send out the email. An SMTP server is a server

68
00:05:15,290 --> 00:05:18,980
whose role is to send and receive emails.

69
00:05:19,280 --> 00:05:27,800
Now we're going to use the outlook SMTP servers, and for that, we need to the domain of that server

70
00:05:28,070 --> 00:05:30,110
and the port of the server.

71
00:05:30,770 --> 00:05:32,810
And these two are publicly known.

72
00:05:32,810 --> 00:05:38,760
So all we have to do is create a server variable and point to smtplib.SMTP.

73
00:05:38,760 --> 00:05:47,690
So that is a class and it expects two arguments, the domain.

74
00:05:47,810 --> 00:06:00,830
So smtp.office365.com, that's the domain of outlook and the port is 

75
00:06:01,810 --> 00:06:02,890
587.

76
00:06:03,670 --> 00:06:10,410
Once you have instantiated the server, you want to say server.starttls.

77
00:06:11,380 --> 00:06:17,350
So you are starting the tls protocol, which is a protocol to send emails in an encrypted form.

78
00:06:18,310 --> 00:06:22,300
And then you want to say server, so you always point to the server now.

79
00:06:22,310 --> 00:06:26,260
dott login, with the log in method

80
00:06:26,260 --> 00:06:30,760
you are logging into your email accounts, outlook or Hotmail.

81
00:06:31,030 --> 00:06:40,930
So I'm logging into this email account now using the sender variable so that email address and the password

82
00:06:40,930 --> 00:06:43,480
variable, that password there.

83
00:06:44,320 --> 00:06:53,980
Once you log in, we are ready to use the send mail method, which expects again the sender.

84
00:06:55,230 --> 00:06:56,610
And the receiver.

85
00:06:59,100 --> 00:07:01,140
And the message.

86
00:07:03,180 --> 00:07:12,000
So that message that string, and then finally, we can say server.quit to quit the server.

87
00:07:12,450 --> 00:07:15,510
And yeah, you are ready to run the code.

88
00:07:16,710 --> 00:07:25,140
If there are no errors, then you probably already received the email, so go and check if you got an

89
00:07:25,140 --> 00:07:27,840
email in your receiver account

90
00:07:29,210 --> 00:07:30,170
which is this one here.

91
00:07:30,290 --> 00:07:39,530
Now, be careful if you're using a new outlook or Hotmail account, then it's probable that the email

92
00:07:39,530 --> 00:07:45,410
might be ending up in your spam folder or junk folder.

93
00:07:45,830 --> 00:07:50,450
So that is the case for me, and that's the email.

94
00:07:50,750 --> 00:07:56,660
So you see, the subject is Hello, hello, this is the body of the email I can report.

95
00:07:56,660 --> 00:08:02,900
This is not spam so that next time I'll probably get this email in my inbox.

96
00:08:04,450 --> 00:08:11,230
So this is a Gmail account that's I entered here in the receiver and that's the complete code.

97
00:08:12,490 --> 00:08:20,200
In the next video, I'll show you how to send an e-mail that has HTML instead of plain text.

98
00:08:20,770 --> 00:08:28,020
So basically, you can enter rich elements. You can enter fonts and other elements using HTML.

99
00:08:28,240 --> 00:08:29,970
So let me show you that's in the next video.

100
00:08:30,010 --> 00:08:30,310
See you.

