In this video, we will write a script which gets email addresses from a series as we file it, gets
many email addresses and sent an email to each of these addresses.
Let's do that.
I'm going to fork the Send Single Email repl.
Sends Email to CSV Contacts.
A few things we need to do first is we want to go to secrets and create a new secret.
Remember, password is the key that gives us the password value. For this Gmail address that I'm using,
for this app,
the password is flask7app#.
But this will not work for you because I will change this passwords, even though this is just
a throwaway account, add new secret.
And after you have added the secrets, the password should be available through these commands.
Then we need a CSV file
with some
tests email addresses, you can create one
CSV file right here by going to Add file and say contacts.csv,
and then, as you may know, a CSV file is a say comma separated file, so in a text file, you write
a format that
applications such as a Python application can understand.
So it could be like name and email.
So we have two columns,
and then you can add rows.
Let's say Yash, and for emails
I'm going to use dropmail.me,
dot me.
And I'm going to use this email, so copy that.
And also.
Open another
window for dropmail.me, so I have two email addresses, two different ones.
This one and that, so copy the first one.
Paste it in here.
So make sure there is a comma after the first name.
And the email address and also a comma after name here.
So this is a header of our data.
These are the delta, let's say, Jim
has this other email address.
Copy that, paste it in here.
And that's should do it, so close the csv file.
You see, I'm double-checking it.
The data are there.
So let's go to main.py.
And we are ready to send this email to those users via this email account.
So what can we do?
Well, the receiver will not be a static string anymore, so I'm going to have to delete that receiver.
Let's keep the subject like that to the contents like that for now.
And well, now when we think about doing the same thing multiple times, a for loop comes in our minds.
Therefore, what I'm going to do is I'm going to write a for loop.
But what are we iterating over?
Well, I think it should be some sort of a list or pandas data frame, maybe that accesses these contacts.
Therefore, first, we need to create a pandas data frame out of that
CSV file using the read_csv method of pandas.
And in quotes here we pass contacts.csv, like that.
Now,
before I go any further, I would like to see if this data frame is being generated correctly.
So I want to see the actual data before we build the full loop.
So I'm going to press control, slash or command slash to comment this block of code because all
I need for now is just to know how the data frame looks like.
So I'm going to run the script and see the outputs of the data frame.
Then we can go further and build the for loop.
So this way of programming with steps helps you keep motivated and not get overwhelmed.
So we got an error.
Pandas is not important.
I forgot to report it.
No problem.
Now pandas is going to be installed by the repl.
That's our dataframe, so it looks OK.
This is the header.
You see it's outcasted here on the top.
And these are the indexes, so row with index zero, row with index one.
Yash
and Jim, right?
Everything is working fine.
So we want to iterate through
Yash
and Jim.
Therefore, I'm going to uncomment this with command slash or control slash and to iterate over a
data frame, you say four index, row.
So these are just two variables you can use whatever you like in the df, is this data frame, iterrows.
With double R.
So now what do we want to do in each iteration?
Hmm.
Do we want to create a protocol in each iteration?
So do you think this is a good idea to have it here, which means that we're logging in into our Gmail
account in every and each iteration?
So if we had like 100 iterations, we would have to log in 100 times.
That is a bad idea.
So I'm going to cut this and log in before we iterate.
So perhaps somewhere here makes sense.
So we have the sender, the subject, the contents, then we log in and then.
Let me delete this.
Then we send multiple emails.
So in the first iteration, we will send an email to row...
So what is the row and what is index?
We can check those.
With print index, so let's print index first and execute.
So, that print out was from visit data frame, and then we have indexes, so this is the index,
the first iteration, we get index zero.
And then we get index one in the second iteration.
And if you print the row, that will be the actual.
So row number one with index one.
And the second row.
So these are the actual rules of the data frame.
And if you go even further and let's say you print out.
Email
of that row.
Let me comment this not to confuse you.
Let's see what email we'll print out.
So email is the actual email of that particular row.
So with that, we can now do this print statement and uncomment that.
And here we say row, square brackets, email in quotes, close square brackets.
So we send an email to each email address.
So that column, right and.
Subject is this subject here and then contents here is this contents here.
But I would like to make this contents dynamic.
So what I'll do is I'm going to cut this variable declarations from there and.
Generates a dynamic content here. With dynamic srings, we mean using an f string, so we add f before
this multi-line string and then
we could say something like hi and a placeholder.
So here comes the variable row, the name of the person.
So that is the name column, right?
So in this iteration, it will give the name of the person and place it in the contents.
And then we get the contents and send it out by email through our Gmail account.
Yeah, that looks good to me.
What do you think?
Are there any issues you could see here?
So let's try out.
So I by mistake I put
a password that is not an app passwords on Gmail.
So I'm going to have to change this
secret.
I can do that by using the row editor here. So, you have to be careful inside the quotes.
I'm going to enter the new password that I created for Gmail and paste that password in here.
And careful with the quotes.
So it has to be like that. Save.
Run.
So we got two "Email Sent" printed out, and I think we got the emails as well.
So "Hi Yash" for the first one.
"Hi Jim" for the second email address.
So that was working.
So a question for you.
How do you think we could improve this program even further?
So try to think about that and maybe posting the Q&A.
And I'll talk to you in the next video.
Thank you.