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