1

I am attempting to send an email using Nodemailer and Twilio Sendgrid, following the tutorial here. As far as I can tell I am following the instructions in the tutorial, as well as theNodemailer and Sendgrid documentation. However every time this method is called, the code in the catch block executes, and I get the error Error: Missing credentials for "PLAIN". There are some other posts on Stackoverflow about this error, but nothing that has helped me solved the problem. I've been stuck on this for several hours, and I'd appreciate any insight anyone can offer!

Here is my code:

async function sendEmail(email, code) {
    try{
        const smtpEndpoint = "smtp.sendgrid.net";
        const port = 465;
        const senderAddress = 'Name "contact@mydomain.com"';
        const toAddress = email;
        const smtpUsername = "apikey";
        const smtpPassword = process.env.SG_APIKEY;
        const subject = "Verify your email";

        var body_html = `<!DOCTYPE> 
        <html>
          <body>
            <p>Your authentication code is : </p> <b>${code}</b>
          </body>
        </html>`;

        let transporter = nodemailer.createTransport({
          host: smtpEndpoint,
          port: port,
          secure: true, 
          auth: {
            user: smtpUsername,
            pass: smtpPassword,
          },
          logger: true,
          debug: true,
        });
        
        let mailOptions = {
          from: senderAddress,
          to: toAddress,
          subject: subject,
          html: body_html,
        };

        let info = await transporter.sendMail(mailOptions);
        return { error: false };
    } catch (error) {
    console.error("send-email-error", error);

    return {
      error: true,
      message: "Cannot send email",
    };
  }
}

And here is the log: error log in bash terminal

Thanks!

0 Answers0