Email Authentication error using nodemailer and AWS Lambda

23 Views Asked by At

I'm trying to send an email through nodemailer (a nodejs package). The issue is that while this works on my desktop, it gives me an authentication error when I run the exact same code on AWS Lambda.

Here's the code:

const nodemailer = require('nodemailer');
const otpGenerator = require('otp-generator');
async function main() {
    try {
        let transporter = nodemailer.createTransport({
            service: "hotmail",
            auth: {
                user: '[email protected]',
                pass: 'password'
            }
        });
        
        let otp = await otpGenerator.generate(6, {
          upperCaseAlphabets: false,
          specialChars: false,
        });
        const html = `<html><head></head><body><div>Here's your OTP: <b>${otp}</b><br><br>This is an automated message - please do not respond. </p></body></html>`;
        var subject = "OTP";
        
        var mailOptions = {
          from: '[email protected]',
              to: '[email protected]',
              bcc: '[email protected]',
              subject:  subject,
              html
        };
        const info = await transporter.sendMail(mailOptions);
        console.log(`Email sent: ${info.messageId}`);
    } catch (err) {
        console.error(err);
        throw new Error(err)
    }
}
main();

Anyone got any ideas what i'm missing? I was originally trying to send mail through gmail but it seemed to require a whole lot of extra stuff like 2FA, app passwords and what not but hotmail seemed to work... until i tried it on Lambda and it wont.

0

There are 0 best solutions below