Azure

Constructing an Azure Queue Set off Operate for Sending Emails with Nodemailer

Introduction

In at the moment’s digital age, automated processes are important for enhancing effectivity and productiveness. One such automation is sending emails based mostly on occasions or triggers. On this weblog publish, we’ll discover the best way to create an Azure Queue Set off Operate utilizing Node.js and Nodemailer to ship emails when a message is added to an Azure Storage Queue.

Conditions

Earlier than we dive into the code, be sure to have the next conditions in place.

  1. Azure account: Create a free Azure account if you do not have one.
  2. Azure Storage Account: Arrange an Azure Storage Account to retailer the queue messages.
  3. Node.js and npm: Set up Node.js and npm in your improvement machine.
  4. Visual Studio Code (or any most popular code editor).

Step 1. Create an Azure Queue Storage: Start by creating an Azure Storage Account if you have not already. Contained in the Storage Account, create a queue named “mail-trigger.” This queue will probably be used to set off the Azure Operate each time a message is added.

Step 2. Arrange your Azure Operate App: Create an Azure Operate App within the Azure portal. Be certain to pick the Node.js runtime. As soon as created, navigate to the “Capabilities” part and add a brand new perform. Select the “Queue set off” template and set the queue title to “mail-trigger.”

Step 3. Configure native.settings.json: In your perform app folder, create a file named native.settings.json to retailer native improvement settings. Replace the file along with your Azure Storage Account connection string, account title, and key. Additionally, specify the queue title and Node.js because the runtime.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "YOUR_STORAGE_CONNECTION_STRING",
    "QUEUE_NAME": "mail-trigger",
    "ACCOUNT_NAME": "YOUR_STORAGE_ACCOUNT_NAME",
    "ACCOUNT_KEY": "YOUR_STORAGE_ACCOUNT_KEY",
    "FUNCTIONS_WORKER_RUNTIME": "node"
  }
}

Step 4. Configure host.json: In the identical folder, create a file named host.json and configure it with the mandatory settings for utility insights and extensions.

{
  "model": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Capabilities.ExtensionBundle",
    "model": "[3.*, 4.0.0)"
  },
  "concurrency": {
    "dynamicConcurrencyEnabled": true,
    "snapshotPersistenceEnabled": true
  }
}
  • Version: Indicates the version of the Azure Functions runtime. In this case, it’s version “2.0.”
  • logging: Configures logging settings for the Azure Functions application.
  • applicationInsights: Configures Application Insights integration for logging and monitoring.
  • samplingSettings: Specifies settings for log sampling.
  • isEnabled: Enables or disables log sampling. If true, log sampling is enabled.
  • excludedTypes: Specifies log types to exclude from sampling. In this case, “Request” logs are excluded.
  • extensionBundle: Configures the Azure Functions extension bundle.
  • id: Specifies the ID of the extension bundle.
  • version: Specifies the version range of the extension bundle to use. In this case, it’s set to a range from version 3.x to version 4.0.0.
  • concurrency: Configures concurrency settings for the Azure Functions application.
  • dynamicConcurrencyEnabled: Enables or disables dynamic concurrency, allowing the system to adjust concurrency levels based on demand.
  • snapshotPersistenceEnabled: Enables or disables snapshot persistence, which helps maintain the state between function executions.

Step 5. Configure function.json: Update the function.json file to specify the queue trigger binding.

{
  "bindings": [
    {
      "name": "myQueueItem",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "mail-trigger",
      "connection": "AzureWebJobsStorage"
    }
  ]
}

Step 6. Write the Azure Queue Set off Operate Code: Now, create a file named index.js and implement the Azure Queue Set off Operate utilizing Nodemailer.

const nodemailer = require("nodemailer");

module.exports = async perform (context, myQueueItem) {
  context.log("JavaScript queue set off perform processed work merchandise", myQueueItem);
  const transporter = nodemailer.createTransport({
    host: "smtp.office365.com",
    port: 587,
    ssl: true,
    tls: true,
    auth: {
      consumer: "[email protected]",
      cross: "***********",
    },
  });

  const mailOptions = {
    from: "[email protected]",
    to: myQueueItem,
    topic: "Take a look at E-mail",
    textual content: "Whats up, it is a take a look at electronic mail from Node.js utilizing Nodemailer!",
  };

  transporter.sendMail(mailOptions, (error, data) => {
    if (error) {
      console.error("Error sending electronic mail:", error);
    } else {
      console.log("E-mail despatched:", data.response);
    }
  });
};

The code has been finalized. Please execute it by urgent F5,

Proceed by initiating the perform, after which embody your electronic mail deal with within the Azure Queue Storage.

Queue

Now, confirm that your perform app is receiving your electronic mail ID from the queue storage.

Function triggered

The perform has been efficiently triggered. Please verify your electronic mail for the message despatched from the perform app.

Test mail

Step 7. Deploy your perform app to Azure utilizing Visual Studio Code or your most popular deployment methodology. As soon as deployed, add a message to the “mail-trigger” queue within the Azure Storage Account. Examine the Azure Operate logs to see the processing particulars and confirm the e-mail despatched to the required deal with.

Conclusion

Effectively finished! You have got efficiently developed an Azure Queue Set off Operate utilizing Node.js and Nodemailer for sending emails triggered by messages in an Azure Storage Queue. This implementation provides versatility for dealing with numerous automation situations inside your functions. Delve into extra Azure companies and options to additional increase and refine your automated workflows. In case you have any questions or considerations, please be at liberty to share them within the feedback part.

Know extra about our firm at Skrots. Know extra about our companies at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button