Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Course conclusion and review
Lecture: Review: Sending an email with SendGrid
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
When we received an order through WhatsApp and called into our API,
0:03
we wanted to send a receipt to let the users know.
0:07
Here's your official receipt for purchasing our cake.
0:11
So what we're gonna do is you send grid another Twilio service to send it.
0:15
So what we do is we create a dictionary that's going to allow us to pass
0:19
data over to a Jinja template.
0:20
Remember, we could do all sorts of things to come up with that HTML.
0:24
But a really flexible thing is suggest leverage Jinja to actually generate those.
0:29
And in order for Jinja to work,
0:30
give it a dictionary of values and it can dynamically generate the HTML just like it
0:35
does in the Web itself. We got our HTML text will create the Send Grid
0:40
API client. We pass over the API Key,
0:43
which were storing secretly. Remember that our secrets.json, we create 'from' 'to' and subject
0:49
object, passing over our information. The to is order.users.email, following that
0:54
SQLAlchemy relationship. But our HTML content we're going to generate our text content by
1:00
just mark down file our HTML content and then we're gonna create our mail message with
1:06
a sendgrid mail class.
1:07
Just pass all the items over.
1:09
Go to the clients they send when you get a response.
1:12
And as long as the response.status_code is 200, 201 or 202
1:15
we're all good. If it's something else,
1:17
we're gonna say there's an error and that's it.
1:20
Incredibly simple to send a really nice email and in practice,
1:24
remember, we also created an attachment,
1:26
pdf attachment, and we attached that as well.
1:29
So really powerful email system here with Sendgrid.