ok so I set up an Ionic webapp with a contact form and I have the form interacting with firebase meaning all my form info is being stored on the real time database. Now I have setup SendGrid according to this tutorial:
Firestore - Database Triggered Events; https://fireship.io/lessons/sendgrid-transactional-email-guide/
However the cloud functions are not being triggered when new data is being entered. I am not getting any errors on the console and from sendgrid dashboard there are no requests. My understanding is that when there is change in the database it will automatically trigger the function and then sendgrid will send emails with the relevant data.
Here is my code;
// Firebase Config
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
// Sendgrid Config
import * as sgMail from '@sendgrid/mail';
const API_KEY = functions.config().sendgrid.key;
const TEMPLATE_ID = functions.config().sendgrid.template;
sgMail.setApiKey(API_KEY);
// Emails the author when a new messages is added
export const newMessage = functions.firestore.document('messages/{messageId}').onCreate( async (change, context) => {
// Raw Data
// const post = postSnap.data();
const msgData = change.data();
// Email
const msg = {
to: msgData.email,
from: 'Your_email@gmail.com',
templateId: TEMPLATE_ID,
dynamic_template_data: {
subject: 'New Message',
name: msgData.name,
text: `Here is the message: ${msgData.message}`,
phone: msgData.phone
},
};
// Send it
return sgMail.send(msg);
});
Deployment of the functions was successful to firebase.
Please any help is appreciated.
edit //////////////////////////////////////////////// edit
Ended up using Nodemailer instead.