Using SendGrid With Azure Functions to Send Mobile App Survey Data


February 21, 2018
Written by
SendGrid Team
Contributor
Opinions expressed by Twilio contributors are their own

Using SendGrid With Azure Functions to Send Mobile App Survey Data

Note: This post is part of SendGrid’s Show & Tell blog series highlighting the awesome things developers in our community are doing. Check out our libraries and open source projects on Github

The company I work for specializes in IT contracting for the Department of Defense (DoD) and other government entities to build secure, scalable, and intuitive services and applications. This means that we are often working within monolithic and policy-ridden infrastructures filled with roadblocks.

This post will discuss the process of creating that solution using SendGrid and an Azure serverless function to send emails.

The problem we needed to solve

Sending emails from your mobile app should be easy, right? Well, that’s not always the case, especially if you write applications for government customers. For this particular use case, we built a mobile app for our customer, a DoD military command, which they released to the fleet to provide them with a career reference application within their organization.

This was a fairly basic informational reference app that pulls down JSON data from a public-facing API and displays a “career path” within their organizational units. Our customers wanted an in-app survey mechanism to directly and securely collect and send that data.

Getting around these roadblocks usually means trying to find and use resources like SendGrid and other compliant tools to ensure our solutions are secure and can scale without hassle. For security purposes, our customer does not have any public-facing mail servers that we can reach from a mobile app.

Fortunately, we could employ the SendGrid API and Azure functions to overcome this specific roadblock without expending too much in overhead cost and technical debt.

How we did it

  1. Configured SendGrid and Azure – We leveraged our existing Azure account, and a free SendGrid account to get things going. We found SendGrid in the Azure Marketplace. Once the account was created, we noted our SendGrid API key to use it later as a function environment variable.
  2. Created a ‘sendEmail’ App Service –We created a new function app service. This generates a storage account to store the function code.


3. Added a SendGrid API key to App Settings - Deploy the function, choose your new app service and select "Application Settings" to add your SendGrid API key. Name it  ‘SendGridKey’, and save the settings.

4. Created a ‘sendEmail’ function – There are several ways to build out the function for generating an email. We used a generic HTTP trigger for our purposes, but there are many templates to choose from, including a pre-configured SendGrid function template.



Specify the language you want to write your function with, a unique name, and the level of authorization you want to give it, and then create the function. We used function-level authorization for our purposes. Access the code editor view on the left-sidebar drop-down. You should have a view that looks like this for your new HTTP trigger function:



5) Modified the function code – First, we removed the boiler-plate code function code so that ‘Task’ was an empty function. We also changed the function signature to a void, non-asynchronous one with these parameters:



Next, we reference the SendGrid library by adding the following code snippet to the top of the function:



This will ensure the SendGrid libraries are referenced and available to the function code. Then, we added the rest of required code to generate an email object:



In the code above, we instantiate a SendGrid Message object with the email subject line of ‘Survey Data’ and set the content type to ‘text/html’. We then take our input parameter and set the message content value to the survey data.

6) Configured ‘function.json’ to handle SendGrid metadata – In the right-hand toolbar, under the ‘View Files’ context, there should be two file items listed there, ‘run.csx’, and ‘function.json’. Select the ‘function.json’, and you should see something that looks like this:



These are declarative string bindings that can be used to interact with your function code and remove the need for hard-coded values. We edited these binding values to configure our outgoing email message:



That’s it. All that is left to do is test your new function by making a POST call to your function URL with your generated key and an HTML payload in the request body. In our specific use case, when a user submits their survey, the app generates that data payload which gets ingested by our function, which in turn, acts as a proxy to the SendGrid API.

Tools like SendGrid are important to us

Cloud-based tools like SendGrid are especially important to those working in restricted environments without many public-facing resources. They provide a simplified and decoupled solution that allows us to focus on our code without having to cobble together something that could take months to get through the red-tape of the government IA process. Sending emails from your mobile app should be easy, and with SendGrid, it is!

To learn more about the ways other developers are using SendGrid, check out our other Show & Tell blog posts.

Recommended For You



Most Popular


Send With Confidence

Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.