Search code examples
node.jsfirebasegoogle-cloud-functionssendgrid

SendGrid unable to send email, firebase functions. (code: 403)


I am using Firebase Functions to host the SendGrid implementation and I am not able to send emails with the emails that are not in the "Single Sender Verification" list in SendGrid .

I am getting the following error when trying to send email from randomemail@gmail.com:

at node_modules/@sendgrid/client/src/classes/client.js:146:29 at processTicksAndRejections (internal/process/task_queues.js:95:5) code: 403

I am able to send the emails from the emails that I added to the Single Sender Verification list in the SendGrid.

Here is my firebase function implementation, please help. I think the problem is related to domain cross origin or something with SandGrid.

import * as functions from "firebase-functions";
const sgMail = require("@sendgrid/mail");

exports.emailMessage = functions.https.onCall((data) => {
    sgMail.setApiKey("my key is here");

   const msg = {
    to: 'stanmozolevskiy@gmail.com', //here is one of the emails that I added to the Singlie Sender verification
    from: data.sender, // only works with email from 
    subject: `${data.name} sent you email`,
    text: data.message,
    html: `<strong>${data.message}</strong>`,
  }
  sgMail
    .send(msg)
    .then(() => {
      console.log('Email sent');
    })
    .catch((error:any) => {
      console.error(error);
    })
    return{"status": "sucess", data: data};
});

Solution

  • Sendgrid allows only emails sent from a a verified email, but you can use the replyTo property in order to solve the problem.

    const msg = {
        to: verifiedEmail,
        from: verifiedEmail,
        **replyTo: data.sender,** 
        subject: `${data.name} sent you email`,
        text: data.message,
        html: `<strong>${data.message}</strong>`,
      }