Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dynamic Template Support #724

Merged
210 changes: 194 additions & 16 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ This documentation provides examples for specific use cases. Please [open an iss

# Table of Contents

* [Email - Attachments](#attachments)
* [Email - Kitchen Sink - an example with all settings used](#kitchensink)
* [Email - Send a Single Email to Multiple Recipients](#singleemailmultiplerecipients)
* [Email - Send a Single Email to a Single Recipient](#singleemailsinglerecipient)
* [Email - Send Multiple Emails to Multiple Recipients](#multipleemailsmultiplerecipients)
* [Email - Transactional Templates](#transactional-templates)
* [Transient Fault Handling](#transient-faults)
* [How to Setup a Domain Whitelabel](#domain-whitelabel)
* [How to View Email Statistics](#email-stats)
* [How to transform HTML to plain text](#html-to-plain-text)
- [Table of Contents](#table-of-contents)
- [Attachments](#attachments)
- [Kitchen Sink - an example with all settings used](#kitchen-sink---an-example-with-all-settings-used)
- [Send a Single Email to Multiple Recipients](#send-a-single-email-to-multiple-recipients)
- [Send a Single Email to a Single Recipient](#send-a-single-email-to-a-single-recipient)
- [Send Multiple Emails to Multiple Recipients](#send-multiple-emails-to-multiple-recipients)
- [Transactional Templates](#transactional-templates)
- [With Mail Helper Class](#with-mail-helper-class)
- [Without Mail Helper Class](#without-mail-helper-class)
- [_Legacy_ Transactional Templates](#legacy-transactional-templates)
- [Legacy Template With Mail Helper Class](#legacy-template-with-mail-helper-class)
- [Legacy Template Without Mail Helper Class](#legacy-template-without-mail-helper-class)
- [Transient Fault Handling](#transient-fault-handling)
- [RetryCount](#retrycount)
- [MinimumBackOff](#minimumbackoff)
- [MaximumBackOff](#maximumbackoff)
- [DeltaBackOff](#deltabackoff)
- [Examples](#examples)
- [How to Setup a Domain Whitelabel](#how-to-setup-a-domain-whitelabel)
- [How to View Email Statistics](#how-to-view-email-statistics)
- [How to transform HTML to plain text](#how-to-transform-html-to-plain-text)

<a name="attachments"></a>
# Attachments
Expand Down Expand Up @@ -469,11 +480,178 @@ namespace Example
```

<a name="transactional-templates"></a>
# (LEGACY) Transactional Templates
# Transactional Templates

IF YOU ARE USING OUR NEW TEMPLATES, PLEASE SEE [THIS ISSUE](https://github.com/sendgrid/sendgrid-csharp/issues/716).
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/Create_and_edit_dynamic_transactional_templates.html).
Following is the template content we used for testing.

For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
Template ID (replace with your own):

```text
d-d42b0eea09964d1ab957c18986c01828
```

Email Subject:

```text
Dynamic Subject: {{subject}}
```

Template Body:

```html
<html>
<head>
<title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the dynamic template feature!
<br /><br/>
I hope you are having a great day in {{location.city}} :)
<br /><br/>
</body>
</html>
```

## With Mail Helper Class

```csharp
using Newtonsoft.Json;
using SendGrid;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
using System;

namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}

static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage();
msg.SetFrom(new EmailAddress("[email protected]", "Example User"));
msg.AddTo(new EmailAddress("[email protected]", "Example User"));
msg.SetTemplateId("d-d42b0eea09964d1ab957c18986c01828");

var dynamicTemplateData = new ExampleTemplateData
{
Subject = "Hi!",
Name = "Example User",
Location = new Location
{
City = "Birmingham",
Country = "United Kingdom"
}
};

msg.SetTemplateData(dynamicTemplateData);
var response = await client.SendEmailAsync(msg);
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadLine();
}

private class ExampleTemplateData
{
[JsonProperty("subject")]
public string Subject { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("location")]
public Location Location { get; set; }
}

private class Location
{
[JsonProperty("city")]
public string City { get; set; }

[JsonProperty("country")]
public string Country { get; set; }
}
}
}
```

Methods also exist on `MailHelper` to create dynamic template emails:
* `CreateSingleTemplateEmail`
* `CreateSingleTemplateEmailToMultipleRecipients`
* `CreateMultipleTemplateEmailsToMultipleRecipients`

## Without Mail Helper Class

```csharp
using Newtonsoft.Json;
using System.Threading.Tasks;
using System;
using SendGrid;

namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}

static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);

string data = @"{
'personalizations': [
{
'to': [
{
'email': '[email protected]'
}
],
'dynamic_template_data': {
'subject': 'Hi!',
'name': 'Example User',
'location': {
'city': 'Birmingham',
'country': 'United Kingdom'
}
}
}
],
'from': {
'email': '[email protected]'
},
'template_id': 'd-d42b0eea09964d1ab957c18986c01828'
}";
var json = JsonConvert.DeserializeObject<Object>(data);
var response = await client.RequestAsync(method: SendGridClient.Method.POST,
requestBody: json.ToString(),
urlPath: "mail/send");
Console.WriteLine(response.StatusCode);
Console.WriteLine(response.Headers.ToString());
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadLine();
}
}
}
```

<a name="legacy-transactional-templates"></a>
# _Legacy_ Transactional Templates

For this example, we assume you have created a [legacy transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.

Template ID (replace with your own):

Expand Down Expand Up @@ -507,7 +685,7 @@ I hope you are having a great day in -city- :)
</html>
```

## With Mail Helper Class
## Legacy Template With Mail Helper Class

```csharp
using SendGrid;
Expand Down Expand Up @@ -546,7 +724,7 @@ namespace Example
}
```

## Without Mail Helper Class
## Legacy Template Without Mail Helper Class

```csharp
using Newtonsoft.Json;
Expand Down Expand Up @@ -707,4 +885,4 @@ namespace Example {
}
}

```
```
Loading