1
00:00:00,270 --> 00:00:07,830
In this video, we will write a script which gets email addresses from a series as we file it, gets

2
00:00:07,920 --> 00:00:14,550
many email addresses and sent an email to each of these addresses.

3
00:00:15,450 --> 00:00:16,050
Let's do that.

4
00:00:17,970 --> 00:00:22,110
I'm going to fork the Send Single Email repl.

5
00:00:25,760 --> 00:00:31,610
Sends Email to CSV Contacts.

6
00:00:37,600 --> 00:00:45,250
A few things we need to do first is we want to go to secrets and create a new secret.

7
00:00:46,660 --> 00:00:54,310
Remember, password is the key that gives us the password value. For this Gmail address that I'm using,

8
00:00:54,310 --> 00:00:54,970
for this app,

9
00:00:56,080 --> 00:00:59,740
the password is flask7app#.

10
00:01:01,760 --> 00:01:06,890
But this will not work for you because I will change this passwords, even though this is just

11
00:01:06,890 --> 00:01:10,580
a throwaway account, add new secret.

12
00:01:10,670 --> 00:01:16,700
And after you have added the secrets, the password should be available through these commands.

13
00:01:18,620 --> 00:01:22,040
Then we need a CSV file

14
00:01:23,070 --> 00:01:23,730
with some

15
00:01:24,880 --> 00:01:29,080
tests email addresses, you can create one

16
00:01:30,130 --> 00:01:38,080
CSV file right here by going to Add file and say contacts.csv,

17
00:01:41,670 --> 00:01:49,230
and then, as you may know, a CSV file is a say comma separated file, so in a text file, you write

18
00:01:49,230 --> 00:01:51,240
a format that

19
00:01:52,260 --> 00:01:56,280
applications such as a Python application can understand.

20
00:01:58,140 --> 00:02:00,810
So it could be like name and email.

21
00:02:02,580 --> 00:02:04,280
So we have two columns,

22
00:02:04,320 --> 00:02:05,740
and then you can add rows.

23
00:02:07,410 --> 00:02:10,220
Let's say Yash, and for emails

24
00:02:10,229 --> 00:02:11,700
I'm going to use dropmail.me,

25
00:02:11,700 --> 00:02:13,890
dot me.

26
00:02:14,100 --> 00:02:17,160
And I'm going to use this email, so copy that.

27
00:02:18,060 --> 00:02:18,810
And also.

28
00:02:20,740 --> 00:02:22,390
Open another

29
00:02:24,480 --> 00:02:30,120
window for dropmail.me, so I have two email addresses, two different ones.

30
00:02:31,910 --> 00:02:35,540
This one and that, so copy the first one.

31
00:02:36,350 --> 00:02:37,550
Paste it in here.

32
00:02:38,970 --> 00:02:42,750
So make sure there is a comma after the first name.

33
00:02:43,980 --> 00:02:48,570
And the email address and also a comma after name here.

34
00:02:48,630 --> 00:02:50,610
So this is a header of our data.

35
00:02:51,210 --> 00:02:53,220
These are the delta, let's say, Jim

36
00:02:54,680 --> 00:02:56,540
has this other email address.

37
00:02:56,900 --> 00:02:59,720
Copy that, paste it in here.

38
00:03:01,610 --> 00:03:05,630
And that's should do it, so close the csv file.

39
00:03:06,290 --> 00:03:08,260
You see, I'm double-checking it.

40
00:03:08,270 --> 00:03:09,200
The data are there.

41
00:03:09,650 --> 00:03:11,150
So let's go to main.py.

42
00:03:11,150 --> 00:03:21,020
And we are ready to send this email to those users via this email account.

43
00:03:21,800 --> 00:03:23,720
So what can we do?

44
00:03:23,750 --> 00:03:30,710
Well, the receiver will not be a static string anymore, so I'm going to have to delete that receiver.

45
00:03:31,820 --> 00:03:36,280
Let's keep the subject like that to the contents like that for now.

46
00:03:37,040 --> 00:03:46,820
And well, now when we think about doing the same thing multiple times, a for loop comes in our minds.

47
00:03:47,660 --> 00:03:52,190
Therefore, what I'm going to do is I'm going to write a for loop. 

48
00:03:52,190 --> 00:03:56,150
But what are we iterating over?

49
00:03:57,260 --> 00:04:06,050
Well, I think it should be some sort of a list or pandas data frame, maybe that accesses these contacts.

50
00:04:06,770 --> 00:04:12,470
Therefore, first, we need to create a pandas data frame out of that

51
00:04:12,480 --> 00:04:16,399
CSV file using the read_csv method of pandas.

52
00:04:17,420 --> 00:04:23,180
And in quotes here we pass contacts.csv, like that.

53
00:04:23,180 --> 00:04:24,560
Now,

54
00:04:24,570 --> 00:04:30,680
before I go any further, I would like to see if this data frame is being generated correctly.

55
00:04:30,800 --> 00:04:35,390
So I want to see the actual data before we build the full loop.

56
00:04:35,720 --> 00:04:43,370
So I'm going to press control, slash or command slash to comment this block of code because all

57
00:04:43,370 --> 00:04:49,070
I need for now is just to know how the data frame looks like.

58
00:04:49,550 --> 00:04:55,310
So I'm going to run the script and see the outputs of the data frame.

59
00:04:55,770 --> 00:04:59,330
Then we can go further and build the for loop.

60
00:05:00,800 --> 00:05:08,240
So this way of programming with steps helps you keep motivated and not get overwhelmed.

61
00:05:08,690 --> 00:05:09,920
So we got an error.

62
00:05:10,400 --> 00:05:11,750
Pandas is not important.

63
00:05:11,750 --> 00:05:12,800
I forgot to report it.

64
00:05:13,880 --> 00:05:14,450
No problem.

65
00:05:14,450 --> 00:05:20,210
Now pandas is going to be installed by the repl.

66
00:05:26,230 --> 00:05:29,660
That's our dataframe, so it looks OK.

67
00:05:29,680 --> 00:05:30,290
This is the header.

68
00:05:30,310 --> 00:05:33,520
You see it's outcasted here on the top.

69
00:05:33,550 --> 00:05:39,580
And these are the indexes, so row with index zero, row with index one.

70
00:05:39,760 --> 00:05:40,120
Yash

71
00:05:40,130 --> 00:05:41,220
and Jim, right?

72
00:05:41,620 --> 00:05:42,700
Everything is working fine.

73
00:05:42,700 --> 00:05:44,200
So we want to iterate through

74
00:05:44,230 --> 00:05:44,590
Yash

75
00:05:44,680 --> 00:05:45,310
and Jim.

76
00:05:47,140 --> 00:05:55,180
Therefore, I'm going to uncomment this with command slash or control slash and to iterate over a

77
00:05:55,180 --> 00:05:58,840
data frame, you say four index, row.

78
00:05:59,020 --> 00:06:06,280
So these are just two variables you can use whatever you like in the df, is this data frame, iterrows.

79
00:06:08,410 --> 00:06:09,550
With double R.

80
00:06:10,990 --> 00:06:15,880
So now what do we want to do in each iteration?

81
00:06:16,510 --> 00:06:16,990
Hmm.

82
00:06:17,170 --> 00:06:21,550
Do we want to create a protocol in each iteration?

83
00:06:22,420 --> 00:06:30,670
So do you think this is a good idea to have it here, which means that we're logging in into our Gmail

84
00:06:30,670 --> 00:06:34,960
account in every and each iteration?

85
00:06:35,350 --> 00:06:40,840
So if we had like 100 iterations, we would have to log in 100 times.

86
00:06:41,140 --> 00:06:42,160
That is a bad idea.

87
00:06:42,160 --> 00:06:47,710
So I'm going to cut this and log in before we iterate.

88
00:06:48,010 --> 00:06:51,940
So perhaps somewhere here makes sense.

89
00:06:53,590 --> 00:06:58,780
So we have the sender, the subject, the contents, then we log in and then.

90
00:07:00,890 --> 00:07:02,060
Let me delete this.

91
00:07:04,760 --> 00:07:07,670
Then we send multiple emails.

92
00:07:07,790 --> 00:07:12,820
So in the first iteration, we will send an email to row...

93
00:07:12,920 --> 00:07:16,190
So what is the row and what is index?

94
00:07:16,910 --> 00:07:17,870
We can check those.

95
00:07:20,900 --> 00:07:26,300
With print index, so let's print index first and execute.

96
00:07:27,850 --> 00:07:36,490
So, that print out was from visit data frame, and then we have indexes, so this is the index,

97
00:07:36,490 --> 00:07:39,460
the first iteration, we get index zero.

98
00:07:39,940 --> 00:07:42,520
And then we get index one in the second iteration.

99
00:07:42,970 --> 00:07:47,140
And if you print the row, that will be the actual.

100
00:07:49,360 --> 00:07:53,740
So row number one with index one.

101
00:07:54,460 --> 00:07:55,820
And the second row.

102
00:07:55,870 --> 00:07:59,500
So these are the actual rules of the data frame.

103
00:08:00,160 --> 00:08:03,790
And if you go even further and let's say you print out.

104
00:08:04,930 --> 00:08:05,620
Email

105
00:08:06,890 --> 00:08:08,060
of that row.

106
00:08:08,450 --> 00:08:11,210
Let me comment this not to confuse you.

107
00:08:11,870 --> 00:08:15,050
Let's see what email we'll print out.

108
00:08:15,290 --> 00:08:18,820
So email is the actual email of that particular row.

109
00:08:20,490 --> 00:08:26,640
So with that, we can now do this print statement and uncomment that.

110
00:08:26,940 --> 00:08:36,450
And here we say row, square brackets, email in quotes, close square brackets.

111
00:08:36,659 --> 00:08:40,049
So we send an email to each email address.

112
00:08:40,950 --> 00:08:44,460
So that column, right and.

113
00:08:46,420 --> 00:08:54,820
Subject is this subject here and then contents here is this contents here.

114
00:08:55,900 --> 00:08:58,930
But I would like to make this contents dynamic.

115
00:08:59,230 --> 00:09:05,320
So what I'll do is I'm going to cut this variable declarations from there and.

116
00:09:07,310 --> 00:09:17,810
Generates a dynamic content here. With dynamic srings, we mean using an f string, so we add f before

117
00:09:17,810 --> 00:09:20,690
this multi-line string and then

118
00:09:22,390 --> 00:09:28,450
we could say something like hi and a placeholder.

119
00:09:28,810 --> 00:09:35,260
So here comes the variable row, the name of the person.

120
00:09:35,650 --> 00:09:37,690
So that is the name column, right?

121
00:09:38,080 --> 00:09:42,550
So in this iteration, it will give the name of the person and place it in the contents.

122
00:09:42,970 --> 00:09:48,850
And then we get the contents and send it out by email through our Gmail account.

123
00:09:50,350 --> 00:09:51,940
Yeah, that looks good to me.

124
00:09:51,970 --> 00:09:53,170
What do you think?

125
00:09:54,990 --> 00:09:58,710
Are there any issues you could see here?

126
00:09:59,790 --> 00:10:00,720
So let's try out.

127
00:10:03,950 --> 00:10:06,920
So I by mistake I put

128
00:10:08,260 --> 00:10:13,360
a password that is not an app passwords on Gmail.

129
00:10:14,560 --> 00:10:16,600
So I'm going to have to change this

130
00:10:17,680 --> 00:10:18,430
secret.

131
00:10:20,850 --> 00:10:28,920
I can do that by using the row editor here. So, you have to be careful inside the quotes.

132
00:10:29,460 --> 00:10:39,240
I'm going to enter the new password that I created for Gmail and paste that password in here.

133
00:10:39,260 --> 00:10:40,860
And careful with the quotes.

134
00:10:40,860 --> 00:10:43,090
So it has to be like that. Save.

135
00:10:44,540 --> 00:10:44,910
Run.

136
00:10:47,880 --> 00:10:54,930
So we got two "Email Sent" printed out, and I think we got the emails as well.

137
00:10:54,940 --> 00:10:57,600
So "Hi Yash" for the first one.

138
00:10:58,410 --> 00:11:00,530
"Hi Jim" for the second email address.

139
00:11:02,130 --> 00:11:03,360
So that was working.

140
00:11:04,470 --> 00:11:06,000
So a question for you.

141
00:11:07,050 --> 00:11:11,370
How do you think we could improve this program even further?

142
00:11:12,480 --> 00:11:15,330
So try to think about that and maybe posting the Q&A.

143
00:11:16,850 --> 00:11:18,620
And I'll talk to you in the next video.

144
00:11:18,740 --> 00:11:19,190
Thank you.

