Azure Functions SendGrid bindings

This article explains how to send email by using SendGrid bindings in Azure Functions. Azure Functions supports an output binding for SendGrid.

This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources:

Example

See the language-specific example:

C# example

The following example shows a precompiled C# function that uses a Service Bus queue trigger and a SendGrid output binding.

[FunctionName("SendEmail")]
public static void Run(
    [ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "ServiceBusConnection")] OutgoingEmail email,
    [SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] out SendGridMessage message)
{
    message = new SendGridMessage();
    message.AddTo(email.To);
    message.AddContent("text/html", email.Body);
    message.SetFrom(new EmailAddress(email.From));
    message.SetSubject(email.Subject);
}

public class OutgoingEmail
{
    public string To { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

You can omit setting the attribute's ApiKey property if you have your API key in an app setting named "AzureWebJobsSendGridApiKey".

C# script example

The following example shows a SendGrid output binding in a function.json file and a C# script function that uses the binding.

Here's the binding data in the function.json file:

{
    "bindings": [
        {
            "name": "message",
            "type": "sendGrid",
            "direction": "out",
            "apiKey" : "MySendGridKey",
            "to": "{ToEmail}",
            "from": "{FromEmail}",
            "subject": "SendGrid output bindings"
        }
    ]
}

The configuration section explains these properties.

Here's the C# script code:

#r "SendGrid"
using System;
using SendGrid.Helpers.Mail;

public static void Run(TraceWriter log, string input, out Mail message)
{
     message = new Mail
    {        
        Subject = "Azure news"          
    };

    var personalization = new Personalization();
    personalization.AddTo(new Email("recipient@contoso.com"));   

    Content content = new Content
    {
        Type = "text/plain",
        Value = input
    };
    message.AddContent(content);
    message.AddPersonalization(personalization);
}

JavaScript example

The following example shows a SendGrid output binding in a function.json file and a JavaScript function that uses the binding.

Here's the binding data in the function.json file:

{
    "bindings": [
        {
            "name": "$return",
            "type": "sendGrid",
            "direction": "out",
            "apiKey" : "MySendGridKey",
            "to": "{ToEmail}",
            "from": "{FromEmail}",
            "subject": "SendGrid output bindings"
        }
    ]
}

The configuration section explains these properties.

Here's the JavaScript code:

module.exports = function (context, input) {    
    var message = {
         "personalizations": [ { "to": [ { "email": "sample@sample.com" } ] } ],
        from: { email: "sender@contoso.com" },        
        subject: "Azure news",
        content: [{
            type: 'text/plain',
            value: input
        }]
    };

    context.done(null, message);
};

Attributes

For precompiled C# functions, use the SendGrid attribute, which is defined in NuGet package Microsoft.Azure.WebJobs.Extensions.SendGrid.

For information about attribute properties that you can configure, see Configuration. Here's a SendGrid attribute example in a method signature:

[FunctionName("SendEmail")]
public static void Run(
    [ServiceBusTrigger("myqueue", AccessRights.Manage, Connection = "ServiceBusConnection")] OutgoingEmail email,
    [SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] out SendGridMessage message)
{
    ...
}

For a complete example, see Precompiled C# example.

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the SendGrid attribute.

function.json property Attribute property Description
type Required - must be set to sendGrid.
direction Required - must be set to out.
name Required - the variable name used in function code for the request or request body. This value is $return when there is only one return value.
apiKey ApiKey The name of an app setting that contains your API key. If not set, the default app setting name is "AzureWebJobsSendGridApiKey".
to To the recipient's email address.
from From the sender's email address.
subject Subject the subject of the email.
text Text the email content.

When you're developing locally, app settings go into the local.settings.json file.

Next steps