3

I want my firebase backend to send an email to me when a document is created in a firestore collection based on a form submission in my vue app..

I found sendgrid to be the easiest to get the job done, the example mentioned in the package page suggests that I store the API key in an Environment variable.
Since this will run from a cloud function, I used the following command firebase functions:config:set sendGrid.key="THE API GOES HERE" as mentioned in Firebase docs here

cloud function
I initialized the firebase cloud functions locally, then I called the admin module so i can listen to onCreate() when a document is created in firestore, I used sendGrid inside the callback function of onCreate().. I tested the code and checked the functions logs in my firebase project and it gets invoked and finished successfully with a status ok, which means that everything should be working fine.

here is my index.js code inside the /functions folder in my project root

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

// sendGrid
const sgMail = require('@sendgrid/mail');

// the cloud function
exports.formSubmitted = functions.firestore.document('message/{messageId}').onCreate(doc => {
  // referencing the form data
  const formData = doc.data();

  // the following should be logged in the function logs in my firebase project 
  console.log(formData);

  // retrieving the environment variable 
  sgMail.setApiKey(functions.config().sendgrid.key);

  // the message to be sent
  const msg = {
    to: 'MY-EMAIL@gmail.com',
    from: formData.email,
    subject: 'new user submitted our contact form',
    text: formData.message,
    html: '<h3> test email from sendGrid </h3>'
  }

  return sgMail.send(msg);
})

result: everything worked fine except I didn't receive the email.

If further code/explanation is needed, please leave a comment below. any help or hints is highly appreciated, thanks in advance.

MoSwilam
  • 646
  • 2
  • 7
  • 22
  • You're going to have a difficult time with this if you don't have a backend that can actually send the mail. – Doug Stevenson Dec 29 '18 at 18:59
  • what do you mean if I don't have a backend? so firebase doesn't have sort of a way to get this done? I doubt that tho! – MoSwilam Dec 29 '18 at 19:02
  • 1
    Firebase, as a platform, does not have an email sending service. You will need a backend and code that up yourself with some email sending service that you choose. – Doug Stevenson Dec 29 '18 at 19:03
  • 1
    I see, this is a bit disappointing, so what are you suggesting? I am actually new to backend in general, I know some Node.js w/ Express, but would it make sense to integrate Express alongside Firebase? this could get messy as you said actually. – MoSwilam Dec 29 '18 at 19:09
  • 1
    You're going to have to do some searches to research this and figure out what your preferred option is. Easiest thing might be Cloud Functions for Firebase. – Doug Stevenson Dec 29 '18 at 19:10
  • you mean to have a cloud function run the Node.js backend code? – MoSwilam Dec 29 '18 at 19:12
  • 1
    That's one of many options you have. – Doug Stevenson Dec 29 '18 at 19:12
  • I understand, well thanks a lot man, though it would be great if you could suggest a certain approach, But thanks anyway.. – MoSwilam Dec 29 '18 at 19:14
  • 1
    Stack Overflow is not an appropriate forum for product suggestions. If you want opinions, try a discussion forum such as Reddit or the firebase-talk newsgroup. – Doug Stevenson Dec 29 '18 at 19:15
  • got it, thanks again :) – MoSwilam Dec 29 '18 at 19:21
  • have you check the sendgrid dashboard "Activity" to see if the sendgrid failed to send the email ? – Umar Hussain Dec 31 '18 at 08:38
  • where can I find that ? – MoSwilam Dec 31 '18 at 08:39
  • Go to your sendgrid https://app.sendgrid.com/ and check in "Activity" tab that the email is sent successfully or not. – Umar Hussain Dec 31 '18 at 08:49
  • On which Firebase payment plan are you? Spark, Flame or Blaze? – Renaud Tarnec Jan 07 '19 at 10:15
  • I am on the Blaze plan, though I got it working already, I didn't change anything in the code actually, the emails were being delivered from the beginning, only to the spam folder, which I still need to figure out why. – MoSwilam Jan 07 '19 at 10:22

0 Answers0