Python-powered Chat Apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: Send the first email: Basic version

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I'm going to finally start sending that email and see if all the stuff we put together about the secrets and the api keys.
0:06 If we got all those details right, let's focus on the bottom bit for just a minute. We're going to create a message and the way we do that as good as
0:13 SendGrid haven't imported it from there. But I'm using the name space. I do like that style. So you know where stuff comes from and it's going
0:20 to take a mail. When I create one of these and in the mail, we got to pass a from potentially multiple two emails the subject.
0:27 Plain text HTML content, some email clients except plain text only. Some will show HTML, and you can give a little better HTML view and a
0:35 little bit better. More specialized plaintext view here and things like that. To notice when need to have the from email address,
0:41 the to email address and so on, and that kind of falls up into this area. So working that would say from=. Now I could possibly say string here.
0:51 It will take different things, but what I'd like to do is be a little more specific and have more control. So I can say sendgrid
0:58 from there's a From and a To and a Subject and all those types of things I want to say 'From' we could put in the email say,
1:07 michael@talkpython.fm and the name. You're the name. Let's just put my name and because From is a keyword will say from_email,
1:19 and we need to do something similar in the to email, but we're going to do a to, and this information is coming through as part of the API's.
1:28 We got the order, which has a user, and the user has an email, and we have an order which still has that user,
1:33 which has a name. The title or the subject will be your order receipt from Cloud City cakes. And HTML. Let's put something really fancy, each one,
1:50 your receipt as an H1, and then maybe we'll do a line break, a new line, line break and new line We'll just say Thanks for ordering.
2:02 I will just say like large chocolate cake or whatever so we'll say large size {order.flavor} cake.
2:11 We'll be, of course, much more specific about this later. And we can also put the text going to be something like this.
2:18 But I'll just say yes, the \n If you're not familiar, that means line break the <br>. We don't want that in there.
2:30 Perfect. So we're gonna order that cake and this is all the details that we need, in order to create our message.
2:38 So now we come down here and say message or mail or whatever you wanna call it sendgrid.mail and look at all the things it takes.
2:45 It takes it from and takes a to makes a subject, plain text content, which we just called text and HTML content,
2:55 which we called HTML. And we don't need anything else. Very cool, right. And then finally, to send the message down here.
3:03 Let's break this up. We'll say response =. Remember the top. We created this client. It has our api_key, client.send And what I'm going to send the
3:15 message. And also just to be a little bit clear, let's go ahead and set the return type so we know exactly what we're working with So what we get back?
3:29 Is this Python, http_client.client.Response. And now that we know what it is, we can go to things like, Hey, it's status code. Perfect.
3:38 And so the final thing we could do, No, no. Still need that down there. Do we? Final thing to do here is we're going to say
3:46 if response.status_code not in {200, 201}. Whoa, something went wrong. We had a problem. Otherwise, we could just print send emails successfully.
4:05 Do we have it, again we solve these other things to do about making fancier content to send and attaching things.
4:12 But here's the basic flow. When a create a client, with our API keys stored secretly, we're going to set up these rich to from subject,
4:20 etcetera. Fields create the message, and then we're going to use the client to send the message,
4:25 get one of these responses back and make sure it's a success oriented status code. If it's not raising their otherwise,
4:31 you know they've already received their email. Everything should be fine. Let's fire this up and see how we're doing.
4:38 Again, we could do this by sending an email through WhatsApp or triggering this API call through
4:44 WhatsApp. But just to save us the effort of filling out all those details, let's just use Postman once again.
4:49 And just so we see something different because it's coming from Michael Kennedy, I'll say I'm Mike Kennedy and this is coming from It's going to go to
4:58 contact@talkPython, just the general like way to reach us. What are we gonna order? We're going to order a red velvet small red velvet
5:07 cake with chocolate popping in lemon frosting. But this this is the stuff that should appear in our email.
5:15 Make sure it's running. It is so without further a do we can press. This should call that function. It should try to talk to send grid,
5:21 try to send us an email, and then momentarily, I better have something in my email client. Oh, no. Look what I did. We got a 202 back and I was saying,
5:33 202 201 is good. So yeah, 202 is totally acceptable. Let me update that and try again. 202 is also a success code.
5:49 Look at that. It returns successfully instead of crashing. That's a really good sign. You can see. Order 7 was sent to Mike Kennedy.
5:57 That's the new person over at contact. Well, let's go check that email address.
6:01 Well, look at that. What we got your order received from Cloud City cakes is in my inbox, and you can see right now it says sent via send
6:12 grid. That's because I haven't authorized or authenticated my domain, But if I did, it would just come straight through.
6:18 As from Michael. And who is it to? It's from michael@talkPython to Mike Kennedy at contact@talkPython.
6:28 Perfect. That is great. And here's our receipt and our message that we sent over. Thanks for ordering your small red velvet cake.
6:36 I'd call that a success, a delicious one.


Talk Python's Mastodon Michael Kennedy's Mastodon