Home > Software engineering >  SendGrid unable to send email, firebase functions. (code: 403)
SendGrid unable to send email, firebase functions. (code: 403)

Time:12-19

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 [email protected]:

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: '[email protected]', //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};
});

CodePudding user response:

I am getting (the following) error when trying to send email from [email protected]:

...

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

This is normal, Sendgrid allows only emails sent from a a verified email, in order to avoid potential malicious users sending emails in the name (i.e. "in the mail") of someone else.


Note that if your sender addresses are coming from the same email domain you can do a Domain Authentication and avoid registering each email address of the domain.

CodePudding user response:

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>`,
  }
  • Related