Fireship · @fireship

SendGrid Transactional Email via a Firestore Cloud Function

Uploaded November 20, 2017 · Length 6:13

SendGrid Transactional Email via a Firestore Cloud Function

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.

  1. 0:00

    sending email is a fundamental feature

  2. 0:01

    that's required by almost every app

  3. 0:03

    today I'm going to show you how to

  4. 0:05

    combine the powers of SendGrid

  5. 0:06

    and firebase cloud functions to send

  6. 0:08

    transactional email to your customers it

  7. 0:11

    works like this a new document is

  8. 0:13

    created in firestore that triggers a

  9. 0:15

    cloud function the cloud function that

  10. 0:16

    makes an API called ascend grid and send

  11. 0:19

    grid sends the emails to your users in

  12. 0:21

    this video we're going to use a fire

  13. 0:22

    stork database trigger but you can also

  14. 0:24

    trigger the function over HTTP and I

  15. 0:27

    already wrote the code for that which is

  16. 0:28

    at angular firebase comm if you're just

  17. 0:30

    finding me for the first time make sure

  18. 0:32

    to subscribe and then head over to

  19. 0:33

    SendGrid to sign up for a free account

  20. 0:35

    it's free if you send less than a

  21. 0:37

    hundred emails per day and then after

  22. 0:38

    that you pay based on volume from there

  23. 0:41

    you'll need to initialize firebase

  24. 0:42

    functions in your project I'm going to

  25. 0:44

    be working in the context of an angular

  26. 0:45

    5 app but this code will work for any

  27. 0:48

    firebase enabled project from there you

  28. 0:50

    can CD into the functions directory then

  29. 0:53

    we'll install send grids nodejs library

  30. 0:55

    send grid has several other packages for

  31. 0:57

    node but the mail package should handle

  32. 0:59

    90% of your use cases if we go into

  33. 1:02

    package JSON it should look something

  34. 1:03

    like this we have sending grid mail and

  35. 1:05

    then I also have cores installed for

  36. 1:07

    HTTP but we're not going to cover that

  37. 1:09

    in this video but check out the link in

  38. 1:11

    the description if you need the HTTP

  39. 1:13

    version of this cloud function from

  40. 1:15

    there you'll want to go into your send

  41. 1:16

    grid account and create a new API key

  42. 1:18

    keep in mind this is a sensitive API key

  43. 1:21

    so don't expose it in your front-end

  44. 1:23

    code or any other public facing

  45. 1:24

    application then you'll set it to your

  46. 1:27

    functions environment by calling

  47. 1:28

    firebase functions config set a send

  48. 1:31

    grid key with your corresponding API key

  49. 1:33

    now before we write the cloud function I

  50. 1:36

    want to show you how I manage

  51. 1:37

    transactional templates in send grid a

  52. 1:39

    template holds all the default HTML and

  53. 1:41

    styles associated with an email and then

  54. 1:43

    we can pass it dynamic data from our

  55. 1:45

    cloud function to display specific user

  56. 1:47

    information inside of the email in this

  57. 1:50

    demo we're going to send an email to the

  58. 1:52

    user every time they receive a new

  59. 1:53

    follower SendGrid has a drag-and-drop

  60. 1:55

    editor that makes editing your templates

  61. 1:57

    very easy managing email HTML and CSS