Search code examples
node.jsangularsendgridsendgrid-templates

Not receiving email from SendGrid


Using Sendgrid, Node.js and Firebase cloud functions, I was able to send emails via http. For the past few days my emails just weren't coming through anymore. I haven't really changed my code much so I have no idea whats causing the emails not to be sent. I checked my firebase function logs but I receive 200 or 204 status codes. When I check sendgrid it shows requests are being made and emails are being sent.

and yes, I checked my spam folder.

Can somebody please help?

Here's my function:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const cors = require("cors")({ origin: true });
const SENDGRID_API_KEY =
  "SENDGRID-APIKEY";
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(SENDGRID_API_KEY);

exports.httpManagerEmail = functions.https.onRequest((req, res) => {
  cors(req, res, () => {
    const toEmail = req.body.toEmail;
    const managerName = req.body.managerName;
    const managerEmail = req.body.managerEmail;
    const managerUUID = req.body.managerUUID;

    const msg = {
      to: toEmail,
      from: {
        email: "[email protected]",
        name: "BLAH"
      },
      text: '',
      html: '',
      templateId: "d-111111111",
      substitutionWrappers: ["{{", "}}"],
      substitutions: {
        managerName: managerName,
        managerEmail: managerEmail,
        managerUUID: managerUUID
      }
    };
    return sgMail
      .send(msg)
      .then(() => res.status(200).send({ message: "email sent!" }))
      .catch(err => res.status(400).send(err));
  });
});

Here is my component.ts

endpoint = "MY_ENDPOINT"

  sendEmail() {
    const managerData = {
      toEmail: this.manager.managerEmail,
      managerName: this.manager.managerFirstName,
      managerEmail: this.manager.managerEmail,
      managerUUID: this.manager.managerUUID
    }
      this.httpClient.post(this.endpoint, managerData).subscribe(data => console.log('email sent: ', data));
    }

Thanks for taking the time to try and help!


Solution

  • Problem solved:

    I investigate the SendGrid docs more thoroughly. I found my problem on the Email Activity page. I saw my emails weren't getting lost, they just weren't being delivered

    I clicked on the log of the emails and this was the error I saw:

    550 5.7.1 Unauthenticated email from mydomain.com is not accepted 
    due to domain's DMARC policy. Please contact the administrator of 
    mydomain.com domain if this was a legitimate mail. 
    Please visit https://support.google.com/mail/answer/2451690 to learn about the 
    DMARC initiative. s5si11407114ywe.292 - gsmtp
    

    the email address I was sending the emails from wasn't authenticated in SendGrid. I needed to complete the Sender Authentication in order for me to use an email from a registered domain proving that I actual own the domain.