Search code examples
node.jsazure-functionsnodemailer

Does Nodemailer SMTP work within azure functions?


I'm trying to run a nodemailer service within an azure function app and it keeps throwing AUTH error. Here's my code below :

require("dotenv").config();
const nodemailer = require("nodemailer");
const getMailIDs = require("./getMailIDs");

module.exports = async function mailSender() {
  let transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    service: "gmail",
    auth: {
      user: process.env.EMAIL,
      pass: process.env.PASSWORD,
    },
  });

  let mailIDs = await getMailIDs();
  for (i = 0; i < mailIDs.length; i++) {
    await transporter.sendMail({
      from: "abc@gmail.com",
      to: `${mailIDs[i]}`,
      subject: "Test Mail",
      text: {
        path: `emailBody.txt`,
      },
    });
  }
};

This is the error thrown :

Error: Missing credentials for "PLAIN"
    at SMTPConnection._formatError (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
    at SMTPConnection.login (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:438:38)
    at C:\Projects\azure\node_modules\nodemailer\lib\smtp-transport\index.js:271:32
    at SMTPConnection.<anonymous> (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:209:17)
    at Object.onceWrapper (events.js:299:28)
    at SMTPConnection.emit (events.js:210:5)
    at SMTPConnection._actionEHLO (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:1319:14)
    at SMTPConnection._processResponse (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:947:20)
    at SMTPConnection._onData (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Projects\azure\node_modules\nodemailer\lib\smtp-connection\index.js:189:44) {       
  code: 'EAUTH',
  command: 'API'
}

It executes when I run this script file alone locally. But throws an error when I call it from index.js file of the azure function app. I'm unsure if using google api/ OAUTH is mandatory for azure function app.


Solution


  • Your error code shared shows that NodeMailers is unable to verify your identity.
      code: 'EAUTH',  command: 'API'
    
    Issue found:
    auth: {
          user: process.env.EMAIL,
          pass: process.env.PASSWORD,
        },
    

    Authentication variables used are environment variable. This works when running locally because you must have an .env file with these variables hosted on your local server.


    Solution:
    You need to add all environment variables under as "New Application Settings" under "Application Settings" in "Configuration" (Settings Panel).

    Doc reference - https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal

    enter image description here

    Add the values as under :

    Name - EMAIL , value - <youremail>
    Name - PASSORD, value - <password>
    



    This should fix the auth issue.