Fireship · @fireship
SendGrid Transactional Email via a Firestore Cloud Function
Uploaded November 20, 2017 · Length 6:13
Summary
send transactional email using sendgrid and firebase cloud functions with example code
- sendgrid transactional email
- firebase cloud functions
- email sending
- firebase firestore
- angular firebase
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
sending email is a fundamental feature
- 0:01
that's required by almost every app
- 0:03
today I'm going to show you how to
- 0:05
combine the powers of SendGrid
- 0:06
and firebase cloud functions to send
- 0:08
transactional email to your customers it
- 0:11
works like this a new document is
- 0:13
created in firestore that triggers a
- 0:15
cloud function the cloud function that
- 0:16
makes an API called ascend grid and send
- 0:19
grid sends the emails to your users in
- 0:21
this video we're going to use a fire
- 0:22
stork database trigger but you can also
- 0:24
trigger the function over HTTP and I
- 0:27
already wrote the code for that which is
- 0:28
at angular firebase comm if you're just
- 0:30
finding me for the first time make sure
- 0:32
to subscribe and then head over to
- 0:33
SendGrid to sign up for a free account
- 0:35
it's free if you send less than a
- 0:37
hundred emails per day and then after
- 0:38
that you pay based on volume from there
- 0:41
you'll need to initialize firebase
- 0:42
functions in your project I'm going to
- 0:44
be working in the context of an angular
- 0:45
5 app but this code will work for any
- 0:48
firebase enabled project from there you
- 0:50
can CD into the functions directory then
- 0:53
we'll install send grids nodejs library
- 0:55
send grid has several other packages for
- 0:57
node but the mail package should handle
- 0:59
90% of your use cases if we go into
- 1:02
package JSON it should look something
- 1:03
like this we have sending grid mail and
- 1:05
then I also have cores installed for
- 1:07
HTTP but we're not going to cover that
- 1:09
in this video but check out the link in
- 1:11
the description if you need the HTTP
- 1:13
version of this cloud function from
- 1:15
there you'll want to go into your send
- 1:16
grid account and create a new API key
- 1:18
keep in mind this is a sensitive API key
- 1:21
so don't expose it in your front-end
- 1:23
code or any other public facing
- 1:24
application then you'll set it to your
- 1:27
functions environment by calling
- 1:28
firebase functions config set a send
- 1:31
grid key with your corresponding API key
- 1:33
now before we write the cloud function I
- 1:36
want to show you how I manage
- 1:37
transactional templates in send grid a
- 1:39
template holds all the default HTML and
- 1:41
styles associated with an email and then
- 1:43
we can pass it dynamic data from our
- 1:45
cloud function to display specific user
- 1:47
information inside of the email in this
- 1:50
demo we're going to send an email to the
- 1:52
user every time they receive a new
- 1:53
follower SendGrid has a drag-and-drop
- 1:55
editor that makes editing your templates
- 1:57
very easy managing email HTML and CSS


