Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Beautiful confirmation emails
Lecture: Concept: Sending an email with SendGrid
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
There are a lot of little details about sending an email a send grid,
0:03
but in reality it's actually really,
0:05
really simple. So what we're gonna do is start by building out the HTML,
0:09
and we were able to leverage the Jinja template engine to do that.
0:12
We'll see more about that later,
0:14
but we just call this build html _function,
0:15
give it the template and then the details dictionary that provides the data.
0:19
To that jinja template. Then we create a send grid client.
0:23
We just set the API key.
0:25
And remember, we did that by storing our secrets secretly and not putting them into
0:29
source control, knowing created from email, a to email on the subject as object oriented
0:34
things out of send grid. So from class, to class and a subject class
0:38
generate the HTML content. We converted the HTML to plain text using html2text
0:44
Then we created a male object with our From, To, Subject and all that information
0:49
and finally just called client.send got a response.
0:52
If the response code or status code was 200 201 or 202,
0:57
most likely, everything was good.
0:58
Otherwise, something bad has happened.
1:01
We're not really sure what, but something bad.
1:03
So we're going to raise an exception.
1:05
Say sorry. Couldn't send that email.
1:08
That's it. And remember, we sent beautiful HTML emails that looked great and professional
1:13
by having this actual HTML generated having the static files back on our site that we
1:18
could reference to you and so on, came out really good.