1

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.

Acer79
  • 183
  • 2
  • 13
  • Check change logs in firebase functions. Did you set up Your project as payment project ? – Mises Oct 04 '19 at 21:03
  • Only functions working with GCP are for free plan. To use SendGrid You need to get payment plan. – Mises Oct 04 '19 at 21:07
  • @Mises how do you setup the project as a payment project? SendGrid says that its free for 100/day. I'm I missing something here? – Acer79 Oct 04 '19 at 21:41
  • Yeah You missing. It's not about SendGrid but Firebase. Firebase Functions are free only when You work with firebase/GCP tools !!! If You want to work with other tools You need to have payment firebase project. !!! – Mises Oct 04 '19 at 21:53
  • 1
    Set up Blaze Plan in Your firebase Project. You will can bandwidth 5gb per month for free and Blaze Plan will harge you only if You cross any free plan so for building webside still shuld be for free. – Mises Oct 04 '19 at 21:58
  • @ Acer79 Read https://firebase.google.com/pricing Outbound networking = Google Services Only And SendGrid is not Google service. – Mises Oct 04 '19 at 22:05
  • I answear the question. Give me a call if it was that. – Mises Oct 04 '19 at 22:10

2 Answers2

3

It's Probobly Free Firebase Spark Plan https://firebase.google.com/pricing. Cloud Functions: Outbound Networking = Google Services Only. If You change to Blaze Plan You still will not pay any thing if You no use much Outbound Networking. I have 2x Blaze Plans 3 months and pay nothing.

Mises
  • 4,251
  • 2
  • 19
  • 32
  • i signed up for the Blaze Plan, but still can't get my function to work. I still get no errors from Firebase, SendGrid, or the console. The data is still being recorded on realtime Database, but still no email from SendGrid. I can't figure it out. – Acer79 Oct 06 '19 at 15:20
  • Did You check are there is an error in functions logs ? – Mises Oct 06 '19 at 16:53
  • console.log response from sgMail.send(msg) And check in logs. – Mises Oct 06 '19 at 17:12
  • Sorry I am still new to firebase but I still doesn't really show me any errors. Here is an image to my log. https://imgur.com/a/8pvDpdS Thank you for your help! – Acer79 Oct 07 '19 at 19:19
  • Try to change authentication policy in your gmail.com address. By default, authentication is restricted and You cannot login through 3rd party applications. – Mises Oct 07 '19 at 20:23
  • https://myaccount.google.com/lesssecureapps here You will less secure gmail so third party application can login to your email. – Mises Oct 07 '19 at 20:25
  • here You have an example how to set up sending email using gmail.com i don't know exacly how SendGrid works i usually use gmail. If SendGrid need to login to Your gmail account You will need to let him in by setting up gmail. Link: https://github.com/firebase/functions-samples/tree/Node-8/email-confirmation – Mises Oct 07 '19 at 20:31
  • Thank you @Mises i was able to get a started by following your link to nodemailer, I stop using SendGrid as I was unable to find out what the error was. – Acer79 Oct 09 '19 at 16:18
  • Check it could be just SendGrid cannot login to Your Email address. XD – Mises Oct 09 '19 at 16:21
  • Using nodemailer and gmail. You will cannot send masive emails. – Mises Oct 09 '19 at 16:22
  • yes I realize this. Google limits 500 recipients a day, but for what im using it for I do not think i will be sending that much emails. Once again thanks for your help it took me 4 days to figure this one out and you were **BIG** help! – Acer79 Oct 09 '19 at 16:44
  • Don't worry i was struggling with it few months a go to. XD – Mises Oct 09 '19 at 17:24
2

ok so this is what worked for me after searching and searching. Thanks to @Mises for giving me a direction to follow. For others that are trying to send transactional emails with firebase using nodemailer here is how I did it.

  1. I followed the above link given to me by @Mises; https://github.com/firebase/functions-samples/tree/Node-8/email-confirmation

I was able to upload the function to firebase, but I was still getting an error in firebase function logs;

-There was an error while sending the email: { Error: Missing credentials for "PLAIN"

So then from there I followed this link; Missing credentials for "PLAIN" nodemailer

unfortunately activating less secure apps on google did not work for me.

aslo offical docs from nodemailer here;

https://nodemailer.com/about/

Hope this helps someone else.

Acer79
  • 183
  • 2
  • 13