How to send emails using an API call.

Even if you are not familiar with the likes of Azure, BizTalk and the Microsoft stack, then you might have heard of web services like IFTTT (‘If This Then That’) which let you connect online services and devices through a chain of instructions to provide new applications and services.

A typical use-case would be to automate recurring functionality that you might otherwise have to action manually. For example, you could set up a simple action that opens the garage door as your car enters the driveway or texts your partner to let them know you’ve left the office (and to get the dinner on!).

Azure supports a technology that uses this and is very similar to the idea behind IFTTT, “If this happens, then, do that.” It is called Logic Apps and is a key component of Azure’s App Services, alongside Web Apps, API Apps & Mobile Apps.

Logic Apps gives you the power to build applications that take input from various sources, process that input via various logical constructs and execute resulting actions. Importantly, in a world that’s becoming more digital by the minute, Logic Apps allows you to connect legacy and cutting-edge systems together.

The Azure Logic Apps service consists of these key components:

  • Managed connector – provides access to the data sources and services that will power the logic app
  • Trigger – a special managed connector that starts a new instance of a workflow when certain conditions are met, such as in incoming HTTP request is received
  • Action – the step after a trigger that makes something happen, such as sending an email
  • Workflow – the combination of the above steps that make up the logic app. This can be constructed and viewed via a graphical interface within the Azure portal or written in JSON as per a normal Azure ARM template

With these fundamental building blocks at your disposal, Logic Apps demonstrates that, from a few simple and logical concepts, you can grow applications of great complexity, power and functionality.

Imagine the possibilities of being able to connect multiple private and public cloud services (via connectors or APIs) to each other. Whether your services are running in Azure (such as SQL Database, Event Hubs, Mobile Apps, Azure Functions, Stream Analytics and even other logic apps) or whether you’re an IoT hobbyist, enterprise architect or Big Data scientist, Azure Logic Apps is a tool only limited by your imagination (and time/money!).

In this five-step tutorial we take you through the creation and execution of a very basic logic app that behaves like an online API for sending emails, such that:

  1. A POST request is made to a callable HTTPS endpoint
  2. The request contains a JSON payload that contains 2 properties, an email subject and body
  3. The email subject and body are sent to an action that constructs an email and sends it using pre-configured credentials to a pre-defined recipient

We are assuming that you already have access to an Azure subscription.

Step 1 – Create the logic app in Azure

Log into the Azure portal and open the Logic Apps blade. Click Add to create a new logic app, giving it a name, selecting the subscription, the resource group and location (the Azure Region where the logic app is running).

Logic Apps - step 1 Add - SystemsUp

Create logic app - step 2 -SystemsUp

Once deployed, select your logic app and click Logic App Designer to bring up a new blade. Scroll down and select Blank Logic App.

Logic App designer 3- SystemsUp

Step 2 – Add the HTTP endpoint trigger to the workflow

The workflow blade should now be displayed. Select the Request/Response trigger.

Logic app - trigger - step 5 - SystemsUp

Step 3 – Configure the HTTP endpoint trigger

Select the trigger and click Use sample payload to generate schema to generate and add the Request Body JSON schema, using the following sample payload:

{
“subject”: “The email subject”,
“body”: “The email body”
}

The result is the following JSON schema:

Logic app - JSON schema - SystemsUp

Step 4 – Add the email action to the workflow

On the workflow, beneath the Request/Response trigger, click New Step and Add an Action.

Logic app - add email to workflow - SystemsUp

In this tutorial we are using an Office 365 email account to send email, so search for the Office 365 Outlook – Send an Email action and sign in with your credentials.

Logic app - Office365 email - Systemsup

Complete the required To, Subject and Body fields as follows (using the subject and body properties from the output of the Request trigger):

Logic app - send email instructions - SystemsUp

Then click Save in the workflow designer blade ribbon to save the workflow to the logic app.

Step 5 – Test the logic app

You are now ready to test your logic app by sending a POST request to the Logic Apps callable endpoint, which can be obtained by looking at the Request trigger and copying the URL.

Logic app - test - SystemsUp

You can check it works by using a free online service for sending HTTP requests, such as hurl.it and filling in the URL, Header and JSON payload details. For example:

Logic app - destination test - SystemsUp

Click Launch Request and you should receive an email that has the subject and body you specified in the JSON payload.

Simple, but powerful!

You have just built a logic app that serves up a callable HTTPS endpoint to which you make an API call, pass email properties and send an email.

For more details on using Logic Apps as callable endpoints see here.

By Andrew Bacon, Cloud Consultant, SystemsUp

SystemsUp is a Microsoft Gold Partner. To find out more about how we help organisations get the best out of the cloud visit our cloud enablement page or join us at Cloud Expo Europe, on the iomart stand – 550 – where we will be demonstrating the value and tools that Azure can bring to your business. 

 

 

 

Related Post