Search code examples
node.jsreactjssendgrid

how to send email confirmation in nodejs and SendGrid?


I have a NodeJs and ReactJs project, where a user can register and after the user is registered they will get an email to confirm their account.

so now when I register the email is working well. but it works with an email that I set in like this.

function sendMail() {
  const msg = {
    to: "someoneemail@gmail.com", 
    from: "myemail@gmail.com", 
    subject: "a subject",
    text: "some text herer",
    html: "<strong>and easy to do anywhere, even with Node.js</strong>",
  };
  sgMail
    .send(msg)
    .then(() => {
      console.log("Email sent");
    })
    .catch((error) => {
      console.error(error);
    });
}

module.exports = { sendMail };

I need to remove this to: "someoneemail@gmail.com" a*

  1. nd instead set the user email, the user who to register on this system

  2. and instead of text: i have to send the token.

so here is the registration part:

router.post("/register", async (req, res) => {
  const { fullName, emailAddress, password } = req.body;
  const user = await Users.findOne({
    where: {
      [Op.and]: [{ fullName: fullName }, { emailAddress: emailAddress }],
    },
  });

  if (user) {
    res.status(400).send({
      error: `some message.`,
    });
  } else {
    bcrypt
      .hash(password, 10)
      .then((hash) => {
        return {
          fullName: fullName,
          emailAddress: emailAddress,
          password: hash,
          isVerified: false,
        };
      })
      .then((user) => {
        const token = TokenGenerator.generate();
        const creator = Verifications.belongsTo(Users, { as: "user" });
        return Verifications.create(
          {
            token,
            user: user,
          },
          {
            include: [creator],
          }
        );
      })
      .then((verification) => {
        console.log("verification", verification);
        sendMail();
      })
      .then(() => res.json("User, Successmessage "));
  }
});

but the codes are not in the same file.


Solution

  • Just add the parameters you need to the sendMail function:

    function sendMail(user, token) {
      const msg = {
        to: user.emailAddress, 
        from: "myemail@gmail.com", 
        subject: "Sending with SendGrid is Fun",
        text: token,
        html: `<strong>${token}</strong>`,
      };
      sgMail
        .send(msg)
        .then(() => {
          console.log("Email sent");
        })
        .catch((error) => {
          console.error(error);
        });
    }
    

    Also inject the needed parameters in the promises:

     .then(async (user) => {
            const token = TokenGenerator.generate();
            const creator = Verifications.belongsTo(Users, { as: "user" });
            await Verifications.create(
              {
                token,
                user: user,
              },
              {
                include: [creator],
              }
            );
            return {user, token};
          })
          .then(({user, token}) => {
            sendMail(user, token);
          })
          .then(() => res.json("User, Successmessage "));