skip to Main Content

I can send mail while in development but mails are not delivered after I deploy to vercel. I have checked that the environment variable there are correct. i am using node js for my backend and react js for my frontend and i am sending email with nodemailer. see my code below

const sendEmail = async({
  to,
  html,
  subject
}) => {
  const transporter = nodemailer.createTransport({
    host: "smtp-mail.outlook.com",
    port: 587,
    secure: false,
    auth: {
      user: process.env.EMAIL_USER,
      pass: process.env.EMAIL_PASS,
    }
  });
  const mailOptions = {
    from: process.env.EMAIL_USER,
    to,
    subject,
    html
  };
  try {
    await transporter.sendMail(mailOptions);
  } catch (error) {
    console.log(error);
  }
};

I have checked the environment variables, which are correct. I get this error on vercel logs:

Error: Invalid login: 535 5.7.3 Authentication unsuccessful
[CH5PR03CA0008.namprd03.prod.outlook.com 2024-05-30T11:35:24.043Z
08DC808625FCE55E] at SMTPConnection._formatError
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:798:19)
at SMTPConnection._actionAUTHComplete
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:1577:34)
at SMTPConnection.
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:1531:18)
at SMTPConnection._processResponse
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:982:20)
at SMTPConnection._onData
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:763:14)
at SMTPConnection._onSocketData
(/var/task/server/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
at TLSSocket.emit (node:events:517:28) at addChunk
(node:internal/streams/readable:368:12) at readableAddChunk
(node:internal/streams/readable:341:9) at Readable.push
(node:internal/streams/readable:278:10) { code: ‘EAUTH’, response:
‘535 5.7.3 Authentication unsuccessful
[CH5PR03CA0008.namprd03.prod.outlook.com 2024-05-30T11:35:24.043Z
08DC808625FCE55E]’, responseCode: 535, command: ‘AUTH LOGIN’ }

2

Answers


  1. Try adding the properties below:

     auth: {
            user: process.env.EMAIL_USER,
            pass: process.env.EMAIL_PASS,
            secureConnection: true,
            tls: { ciphers: 'SSLv3' },
     },
    
    Login or Signup to reply.
  2. I have exactly the same error in deployed express.js backend on Vercel :

    Error: Invalid login: 535 5.7.3 Authentication unsuccessful [MN2PR06CA0003.namprd06.prod.outlook.com 2024-05-31T14:43:25.191Z 08DC8174A56E57CA]
    at SMTPConnection._formatError (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:798:19)
    at SMTPConnection._actionAUTHComplete (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:1577:34)
    at SMTPConnection.<anonymous> (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:1531:18)
    at SMTPConnection._processResponse (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:982:20)
    at SMTPConnection._onData (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:763:14)
    at SMTPConnection._onSocketData (/var/task/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
    at TLSSocket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5) {
    code: 'EAUTH',
    response: '535 5.7.3 Authentication unsuccessful [MN2PR06CA0003.namprd06.prod.outlook.com 2024-05-31T14:43:25.191Z 08DC8174A56E57CA]',
    responseCode: 535,
    command: 'AUTH LOGIN'
    }
    

    Based on the code below using promise to wrap nodemailer.sender as suggested in: Nodemailer in vercel not sending email in production

    router.post("/", async (req, res) => {
    
      const { firstname, lastname, email, phone, message, ownCopy } = req.body;
    
      const contactFields = {
        firstname,
        lastname,
        email,
        message,
        ownCopy,
      };
    
      if (phone) {
        contactFields.phone = phone;
      }
    
      const emailContent = [
        `Nom: ${firstname} ${lastname}`,
        `Email: ${email}`,
        phone ? `Téléphone: ${phone}` : "",
        `Message: ${message}`,
      ]
        .filter(Boolean)
        .join("nn");
    
      let transporter = nodemailer.createTransport({
        host: "smtp-mail.outlook.com",
        port: 587,
        secure: false,
        auth: {
          user: process.env.OUTLOOK_EMAIL,
          pass: process.env.OUTLOOK_PASSWORD,
        },
      });
    
      await new Promise((resolve, reject) => {
        // verify connection configuration
        transporter.verify(function (error, success) {
          if (error) {
            console.log(error);
            reject(error);
          } else {
            console.log("Server is ready to take our messages");
            resolve(success);
          }
        });
      });
    
      const newContact = new Contact(contactFields);
      await newContact.save();
    
      let mailOptions = {
        from: process.env.OUTLOOK_EMAIL,
        to: process.env.OUTLOOK_EMAIL,
        cc: ownCopy ? email : "",
        subject: "New Message from Contact Form",
        text: emailContent,
        //  html: "<b>Hello world?</b>",
      };
    
      await new Promise((resolve, reject) => {
        transporter.sendMail(mailOptions, (error, info) => {
          if (error) {
            reject(error);
            res.status(500).json({
              result: false,
              error: error,
            });
          }
          resolve(info);
          console.log("Message sent: %s", info.messageId);
          console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
          res.status(200).json({ result: true, newContact });
        });
      });
    

    Any advice on this problem?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search