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