1
00:00:00,540 --> 00:00:00,900
Hi.

2
00:00:00,900 --> 00:00:01,620
Welcome back.

3
00:00:01,620 --> 00:00:08,670
In this video, you learn how to send an email through a Django form more specifically.

4
00:00:09,660 --> 00:00:12,480
When the user enters some data here.

5
00:00:17,100 --> 00:00:18,090
Like that.

6
00:00:18,090 --> 00:00:26,040
And then when they press submit, Django should send an email to the user's email address.

7
00:00:26,190 --> 00:00:32,790
So that email, the content of that email will contain the data that the user entered.

8
00:00:32,790 --> 00:00:39,690
So it's like a confirmation email that we sent to the user letting them know that, Oh, you submitted

9
00:00:39,690 --> 00:00:42,570
a job application to our company.

10
00:00:42,600 --> 00:00:44,580
These are the data you submitted.

11
00:00:44,580 --> 00:00:52,080
And so we send that to whatever the user provides here as an email account so the user can enter any

12
00:00:52,110 --> 00:00:53,330
email account here.

13
00:00:53,340 --> 00:00:58,650
But for this you need to have your own email account.

14
00:00:58,650 --> 00:01:04,890
And I do suggest to have a Gmail account, although other accounts should work.

15
00:01:04,890 --> 00:01:07,440
But I will use Gmail myself.

16
00:01:07,440 --> 00:01:15,810
So ideally you need two accounts, one to to use as a user and one to use as the email sender address.

17
00:01:15,840 --> 00:01:16,350
Right.

18
00:01:16,350 --> 00:01:25,030
But even if you have one that will work, so you can use the same email address as a sender and a receiver.

19
00:01:25,030 --> 00:01:28,390
So let's go to Django and see.

20
00:01:28,390 --> 00:01:36,790
We need to import from Django dot core dot mail import email message.

21
00:01:36,790 --> 00:01:44,080
This is a class, so this is integrated inside Django you don't need to use.

22
00:01:44,850 --> 00:01:54,750
And SMTP lib the library because this is a high level module in Django which actually is based on SMTP

23
00:01:54,750 --> 00:02:02,390
lib, but it makes things easier so that it's a more flawless integration with your Django app.

24
00:02:02,400 --> 00:02:09,330
I think it makes sense to have it after we submit the data right to the database and before we display

25
00:02:09,330 --> 00:02:10,740
a message to the user.

26
00:02:10,740 --> 00:02:18,450
So we want to send the email somewhere in here, let's create a message body string first.

27
00:02:18,660 --> 00:02:20,940
Let's use an F string for that.

28
00:02:20,970 --> 00:02:26,370
Let's say a new job application was submitted.

29
00:02:28,940 --> 00:02:30,170
Thank you.

30
00:02:30,230 --> 00:02:33,350
And then here, of course, you can enter multiple data.

31
00:02:33,630 --> 00:02:36,810
So let's enter first name for now.

32
00:02:36,830 --> 00:02:37,870
You can add more.

33
00:02:37,880 --> 00:02:39,580
You can use break lines.

34
00:02:39,590 --> 00:02:47,660
So if you use like backslash n then this will be displayed in the email in the next line.

35
00:02:47,660 --> 00:02:49,490
So you'll have like, thank you.

36
00:02:49,490 --> 00:02:52,340
And then in the next line you'll have the name of the user.

37
00:02:52,340 --> 00:02:56,180
So you can use these backslash N's, but I'll just delete it.

38
00:02:56,180 --> 00:02:58,250
That's okay as it is now.

39
00:02:58,400 --> 00:03:00,650
So that's the email contents.

40
00:03:01,070 --> 00:03:04,280
So we have the variable, but that's just a variable.

41
00:03:04,280 --> 00:03:06,470
So we need to enter that variable somewhere.

42
00:03:07,640 --> 00:03:14,900
Let's create an email message instance using the email message class we imported up here.

43
00:03:15,320 --> 00:03:23,840
So that gets as input the subject form submission confirmation for example, that will be displayed

44
00:03:23,840 --> 00:03:31,260
as the subject of the email that the user will receive of in their email address which they enter here.

45
00:03:31,470 --> 00:03:32,010
Right.

46
00:03:32,010 --> 00:03:34,920
So that's the subject.

47
00:03:35,780 --> 00:03:38,250
Followed by the message body.

48
00:03:38,270 --> 00:03:42,350
So next, you should provide the body of the email.

49
00:03:43,790 --> 00:03:48,200
And then the receiver to a list of receivers.

50
00:03:48,230 --> 00:03:49,910
We only have one.

51
00:03:50,120 --> 00:03:51,540
So email.

52
00:03:51,560 --> 00:03:53,460
So that's this variable.

53
00:03:53,480 --> 00:03:56,900
So we get the email of the user.

54
00:03:56,930 --> 00:04:03,680
Whatever the user enters here, we grab that through this code, we store that in this variable, and

55
00:04:03,680 --> 00:04:09,800
then we get that value of that variable and we pass it here to this argument.

56
00:04:10,250 --> 00:04:14,390
So that will create an instance email message instance.

57
00:04:14,660 --> 00:04:17,660
But we need then to.

58
00:04:18,459 --> 00:04:22,660
Send out that message, that email using the send method.

59
00:04:22,660 --> 00:04:27,160
So email message dot send that should send out the email to the user.

60
00:04:27,340 --> 00:04:27,850
Right.

61
00:04:27,850 --> 00:04:32,270
But we didn't specify anything about the sender.

62
00:04:32,290 --> 00:04:36,760
Where does Django send the email from?

63
00:04:37,000 --> 00:04:42,160
Well, you should specify that in settings.py.

64
00:04:42,520 --> 00:04:53,620
So settings dot p y is inside my site, not inside job application but inside my site, settings.py.

65
00:04:53,860 --> 00:05:06,010
So Settings.py has a series of configuration elements and so you can add more configuration values here.

66
00:05:06,130 --> 00:05:11,230
In this case, that will be a series of variables.

67
00:05:11,270 --> 00:05:12,640
Email backend.

68
00:05:14,140 --> 00:05:21,020
So you see, these variables should be with capital letters and you should type them exactly as you

69
00:05:21,020 --> 00:05:22,340
see it here.

70
00:05:22,340 --> 00:05:25,760
So email underscore backend with capital letters.

71
00:05:25,790 --> 00:05:33,170
Django will look for these variables when it runs the application actually when it sends out an email.

72
00:05:33,470 --> 00:05:44,240
So you want to specify Django dot core dot mail dot back ends.smtp.email back end.

73
00:05:45,030 --> 00:05:46,500
So that's a string.

74
00:05:48,270 --> 00:05:51,090
Then you want to specify the email host.

75
00:05:52,550 --> 00:05:59,480
I'm using gmail, so that should be SMTP gmail.com.

76
00:05:59,510 --> 00:06:00,650
This is the host.

77
00:06:01,420 --> 00:06:09,510
All the Gmail server and then email ports is five eight, seven.

78
00:06:09,520 --> 00:06:23,680
Again, this is the port for Gmail email use TLS You want to specify this to true email host user sorry,

79
00:06:23,680 --> 00:06:24,670
user.

80
00:06:25,000 --> 00:06:31,390
That's your gmail account from which you want to send out emails to the users.

81
00:06:32,050 --> 00:06:37,300
So I'm using this app eight flask at gmail.com.

82
00:06:39,190 --> 00:06:44,320
And then you want to specify the email host password variable.

83
00:06:46,830 --> 00:06:51,230
So for this I have to create a new app in Gmail.

84
00:06:51,240 --> 00:07:00,120
So you want to go here, manage your Google account, go to security, you want to go here, make sure

85
00:07:00,120 --> 00:07:06,000
you have activated two step verification and then go to app passwords.

86
00:07:06,360 --> 00:07:10,860
You may be required to enter your Gmail password here.

87
00:07:10,860 --> 00:07:14,280
So the password you log in to your Gmail account.

88
00:07:15,490 --> 00:07:22,300
And then here we want to create an app password, which is different from the password you used to log

89
00:07:22,450 --> 00:07:23,310
to Gmail.

90
00:07:23,320 --> 00:07:25,480
So go to select app.

91
00:07:25,510 --> 00:07:32,740
Other pick a name, just write something Django application form for example, and press on generate

92
00:07:32,740 --> 00:07:34,750
that should give you the app password.

93
00:07:34,750 --> 00:07:36,760
So copy that.

94
00:07:37,570 --> 00:07:41,980
Go to Django and you want to paste that password in here.

95
00:07:42,400 --> 00:07:51,190
So as a string and that's about how to configure your email address, your sender email address.

96
00:07:52,590 --> 00:07:55,380
So we go back to Views.py.

97
00:07:56,250 --> 00:08:00,750
Things look good to me, so let's give it a try.

98
00:08:02,630 --> 00:08:06,800
Go to the site, reload it first, enter some data.

99
00:08:09,190 --> 00:08:12,430
Employed submit.

100
00:08:13,000 --> 00:08:13,510
Right.

101
00:08:13,510 --> 00:08:20,290
So we got this, but let's see if we actually got the email in our account.

102
00:08:20,290 --> 00:08:27,220
So app Django was the email address of the user and that's the email I received.

103
00:08:27,220 --> 00:08:30,520
So a new job application was submitted.

104
00:08:30,520 --> 00:08:31,150
Thank you.

105
00:08:31,150 --> 00:08:31,660
Audit.

106
00:08:31,660 --> 00:08:37,299
So that's corresponds to this message body here.

107
00:08:39,299 --> 00:08:42,690
Of course you can expand this message if you like.

108
00:08:42,840 --> 00:08:50,700
So as I mentioned already, you can write like Backslash N and perhaps another backslash and in here

109
00:08:50,730 --> 00:08:53,880
then followed by last name.

110
00:08:53,880 --> 00:08:56,880
So give this a try and see what you'll get.

111
00:08:58,680 --> 00:09:01,170
I'll just leave it at this point.

112
00:09:01,170 --> 00:09:06,690
I hope this was useful for you and thanks a lot for following.

113
00:09:06,810 --> 00:09:08,040
I'll see you in the next videos.

