Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Sending Multiple Emails From a CSV File

You may come across the situation, where you want to send a certain message to different contacts. Maybe you also need to slightly change the message for each receiver. In this video, you’ll learn how to use CSV files to do exactly that! In this case you are a teacher, who wants to inform his students about their grades.

00:00 So now you can format your emails and add attachments, but if you want to send these to multiple people, you currently have to change the code each time, which is not ideal.

00:09 In this video, you’ll learn to use a CSV file as an input so that you can fire off a bunch of emails with one script. Over here, I’ve got a contacts.csv file that has the name, email, and grade for a couple of fake people.

00:23 Note the email addresses where I’ve added a plus (+), and then the person’s name to my test email. When Gmail’s routing the email, it’s going to ignore anything after the + sign, so all of these will be sent to my test email but kept separate.

00:38 This is very useful when you’re practicing sending multiple emails at once. So, let’s head back to the editor, and you’re going to start with a blank script this time.

00:47 Grab your imports. You’re going to need the csv library, smtplib, and ssl. Go ahead and make your message template.

00:58 Call that message, do a multiline string, and you’ll say """From: {sender}""", """To: {receiver}""". """Subject: Your Grades""".

01:21 """Hi {name}, your grade is {grade}.""". Our sender will be our email address, but then the receiver, name, and grade will be pulled from that CSV file.

01:36 So, let’s go ahead, make a sender, which in my case is 'catinthehacks@gmail.com'—and actually, that’s 'thecatinthehacks'.

01:50 And password. Like before, use an input().

02:00 Okay. Much of this will be the same. You’ll create a context with ssl, create a default context. Let me bring this down. Okay. With smtplib.SMTP_SSL()and because we’re not really messing around with these anymore, I’m just going to type them in as strings instead of setting variables for them.

02:27 Set your server, your port, make context=context, call this your server,

02:40 then log into that server.

02:48 All right. So up until now, just about all of this should be a review, except now you’re going to use that CSV file. So now open('contacts.csv').

03:03 And just call this file, and then make a reader which is going to be a csv.reader(). Pass in file. You’re going to skip the first row because that’s just header information that we don’t need.

03:18 And then unpack that as for name, email, grade in reader: you’re going to .sendmail(). You need to pass in your sender, the email that it’s going to, and message, which is from the message template above.

03:43 And just open that. sender is going to be sender, email is going to be email, name is name, and grade is grade.

04:00 And actually, before I send this, I think—yeah. This 'To: {receiver}' should actually just be email here to keep all the naming the same.

04:11 Okay! So if this works the way it should, it’ll log into the server like we’ve done before, and then it’s going to go through your CSV file and then for each row in there, it’s going to send an email based on the information that’s in that CSV.

04:23 So, save this and try it out!

04:30 Okay. Passing in the password.

04:36 We don’t see any errors, so let’s hop over. And what do we have here? Okay. So Your Grades, it looks like here’s one for Ron, here’s one for Rabbit, and you can see that the template has been changed for each of them. So this was sent to Ron, and Rabbit, and I think I got one more here. Yeah, there’s the third one there. All right!

04:59 So now you can see that with one script and a CSV file, you can send out multiple emails that are personalized. That’s pretty cool. You can use this kind of system if you need to verify somebody’s email for a website or if you’re running an email list with Python.

05:15 You do need to be careful, though, because it’s very easy to send out way too many spam emails, so this is definitely something to use responsibly. All right!

05:24 In the next video, you’re going to learn how to use Yagmail, which is a library to make working with Gmail even easier. Thanks for watching.

Avatar image for Stephen Paden

Stephen Paden on July 16, 2019

No, this was only after my original file main.py did the same thing.

Avatar image for Joe Tatusko

Joe Tatusko RP Team on July 16, 2019

That’s great! Glad you were able to work it out :D

Avatar image for Stephen Paden

Stephen Paden on July 16, 2019

Nothing worked out lol, it doesn’t work.

Avatar image for Stephen Paden

Stephen Paden on July 16, 2019

I’m sorry, I will figure it out.

Avatar image for Stephen Paden

Stephen Paden on July 16, 2019

It is now working. I made some transcription errors in a few places. Frustration will do that. Thank you Dan and Joe!

Avatar image for Joe Tatusko

Joe Tatusko RP Team on July 16, 2019

Sorry, was replying to RetiringInComo haha

Awesome that you got it too! Glad we (well, mainly Dan :P) could help out.

Avatar image for Stephen Paden

Stephen Paden on July 16, 2019

Yes, I am going to give that link full attention after this fiasco. This really is yet another wonderful tutorial.

Avatar image for beauenslow

beauenslow on Aug. 9, 2019

Is there a way to send multiple emails from a csv file and have the message sent as html. I have been having trouble making this work. Can you help?

Avatar image for Joe Tatusko

Joe Tatusko RP Team on Aug. 13, 2019

Sure thing! This would be similar to RetiringInComo’s question above. You need to structure your code in a way that you’re making an HTML email for each row of the CSV:

open the csv file -> loop through the rows -> create your HTML email object -> send email

Where this gets tricky is what you put in the ‘body’ column of the CSV. I’d start with adding the HTML strings directly into the CSV, then start working at how you could keep the critical information in the CSV and use formatted strings to make an HTML template that is kept outside of the CSV

Avatar image for Kalesis

Kalesis on Sept. 7, 2019

Hello, excellent course.

If with gmail to test we add “my +”, which is the simile for Office365 / Outlook, 7 Yahoo, etc.

Thank you

Become a Member to join the conversation.