Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: Email service layer

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We've created our sendgrid account. We've got our API key's. It's time to start usend the API.
0:06 Now, if you think back to where in our Web application and we wanted to do that, that was when the order was placed over on Twilio studio.
0:13 We've got a final callback to make that happen on our personal API. That's over here in the order api in this order method right there,
0:22 and we even have a friendly little to do says TODO. Send email receipt and invoice. So two main things we have to do send an
0:30 email receipt and generate a PDF invoice and attach it to that email. Just like we have a user service and an order service.
0:37 We're going to have an email service when I go over here and say,
0:44 send_cake_order_receipt(order: Order). I'm going to pass over the order one of these orders which we need to import.
0:52 Okay, so what we need to do is, we need to gather the email details that would be name email and so on,
1:08 generating the HTML content and then finally gather the email details. What's their name, What did they order?
1:21 How much did it cost? We need to actually generate PDF. We need to generate the HTML content of the email we're going to send.
1:29 We're gonna attach the pdf to the email, and we're gonna send it along usend send grid. Sound complicated? It's a little bit,
1:35 but actually, you'll see surprisendly simple for each one of these steps. Now, in order for us to do this, we're going to need to use send grid.
1:46 Except we don't have sendgrid installed. Pycharm will do that for us. Can say, install sendgrid that worked. And then it says, you know,
1:55 this should probably be in your requirements. Yes, yes, it should. So please put it there with our other ones.
2:01 Just like that. Now, this is not set up yet. send grid. We're going to have to get our API key's and configure it before we can send an email.
2:09 But we've got a little bit of work to do before we're ready to work with that. So down here, let's just say a little quick print statement.
2:17 I sent the email about the order to. How do we get the user well, remember, the order has a relationship and we actually do a join.
2:25 So we always have the user associated with the order. So let's put their name right there.
2:33 We want to send this along and let's just make sure that we're calling us. Well, that's it that actually rounds out this function.
2:46 We just got more work to do over here in our email service. Let's go out and test this real quick. Let's go in and just run, again,
2:53 Notice we've moved on to the next chapter of the snapshot of source code that you get out of GitHub. And instead of going through the whole,
3:00 let me fire up what's happened. Asked to order a cake again, We're really just testing this one function, this one api endpoint. And to do that,
3:08 it's really good to just use postman as we did in the beginning. So I'm gonna come over here and say,
3:13 We want to make a call to api/order passend over this information The customer is the number. There's the order. There's a price.
3:22 So let's just send that over. This is what twilio Studio is sending to our API when we talk to
3:27 it through WhatsApp. So we sent all that over and you can see it's sent back information here. Perfect. Now,
3:34 if we go back and look at the print statement, we'll send email about whatever order this is to Michael. Fantastic. So it looks like we have the order.
3:41 The order has the user. Presumably it has the details. It does. We've already seen that in the database.
3:47 So I think our order call over into our email service here is ready to go So what's left to do to make this chapter work?
3:55 Well, there's some steps to be done, but they're all laid out right here.
3:58 Get the email details that's just basically order.user and associated information. But generate the content, generate the PDF,
4:06 set up the API with the secrets, and then send that E-Mail.


Talk Python's Mastodon Michael Kennedy's Mastodon