#100DaysOfWeb in Python Transcripts
Chapter: Days 65-68: Heroku and Python Platform-as-a-Service
Lecture: Use SendGrid to send an email

Login or purchase this course to watch this video and the rest of the course contents.
0:00 With SendGrid imported into Heroku into our repo up there, let's actually go ahead and create the script.
0:09 So this is the script that's actually going to use SendGrid itself to send our emails. And when you take a look at this
0:16 you'll be able to see just how this could be changed to suit many different purposes. So let's create the script now called Sendgrid emailer.py
0:31 and we need to start with import send grid, obviously. That's all we really need to import from Python rather than email or mime or anything like that
0:43 and we just need to import from SendGrid helpers.mail. We're importing email, we're importing content and we're importing mail.
0:56 As you can imagine, we are going to need our api key to do anything, so let's set that up first. We use the same type of syntax
1:04 the SendGrid documentation uses, why differ? And we'll go sendgrid equals or is sendgrid.sendgrid.api client. This is all straight from the docs
1:18 nothing special, api key. And in this space, between two apostrophes there that's where we actually put the api key.
1:27 So the one that you saved when you set up your SendGrid account, put that in there and you should be golden. Now, here's where we set up our email.
1:39 So we have a from email address and we then hook that up in email here. Now, I'm using test@example.com if that'll allow me to send the test email.
1:53 You can set in whatever you want. Subject. I'm going something a little more with a theme because I can't wait for Game of Thrones!
2:06 And then we go to two email, and this is where you simply chuck in whoever you want to send this email to. As you can imagine, as your product
2:16 or whatever it is you're using here this SendGrid add-on for, you can put in a list you can put in multiple email addresses
2:24 it doesn't just have to be one. I'm going to use the PyBites blog email that we used to set up Heroku. Oops, just close that off.
2:36 And then we throw in some content. And you can do whatever you want with this content. I'm going with plain text.
2:46 And then I'm entering in my actual content. So text, plain, and the string I'm going to put in there with a little lame, terrible joke.
3:01 And that's it. Next, we set up mail. So this is where we take all three of these things or sorry, I should say all four of these things
3:10 from email subject to email and content and we put it all together into a mail object. So from email, subject to email and content.
3:26 And then we do our response. This is where it all gets sent off and pushed off or posted, I should say, to SendGrid itself.
3:36 So sg.client.mail.send.post. There's a lot of dots there. Request body equals mail.get. And again, I will repeat, this is just straight
3:50 from the SendGrid documentation. Now, for local purposes, we probably want to print some response codes, just to show that this has happened
4:02 and if this has worked or if it's failed we'll see it here. If you want to actually collect these codes you would probably pipe them out to a file
4:12 that would then be stored in some sort of database or persistent database. So response.body print response.headers.
4:24 This is all recommended from the docs. And that's our script. So we can run that locally. I'm going to save it and quit it here.
4:34 If I just save it for now and I will enter my api key when the video's not recording. And with the api key entered, let's just list this out
4:48 we'll launch the virtual environment, already launched and we have SendGrid installed, pip installed so now we can run Python sendgrid emailer.py.
5:03 And that will actually send the email. The way it works is this will actually send the post of this message through to the SendGrid service
5:11 and they will then push it out. So, once this is sent out, once we've got this response here, we can then go to our actual SendGrid page
5:22 and we can see whether this email has been sent. So let's check that now. And there's SendGrid there, we've launched that
5:30 it tells us hello sender, here's your recent email activity. We popped down and left here to activity and we can see the emails that were sent.
5:40 Once it loads, just look for the timestamp because you probably will see quite a few of these over time. You can see this one here, 5:25 am.
5:48 That's not actually the time here it's just the time zone I have set I haven't configured it. And delivered 5:26.
5:55 So within one minute of processing our script request or our post request, it then sent the email to the PyBite's blog Gmail account.
6:06 And we can bring that up now and have a look at that as well. And there it is! Winter is coming and the lame joke which I won't repeat
6:13 out loud, out of self-respect. And we can see it came from test@example.com and was delivered to PyBites blog plus 100 Days. And that is SendGrid;
6:23 it's actually really simple, isn't it?


Talk Python's Mastodon Michael Kennedy's Mastodon