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

Dynamic templates support #449

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 129 additions & 19 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ This documentation provides examples for specific use cases. Please [open an iss

# Table of Contents

* [Transactional Templates](#transactional_templates)
* [How to Setup a Domain Whitelabel](#domain_whitelabel)
* [How to View Email Statistics](#email_stats)
* [Transactional Templates](#transactional-templates)
* [Legacy Templates](#legacy-templates)
* [How to Setup a Domain Whitelabel](#domain-whitelabel)
* [How to View Email Statistics](#email-stats)

<a name="transactional-templates"></a>
# Transactional Templates
Expand All @@ -13,6 +14,102 @@ For this example, we assume you have created a [transactional template](https://

Template ID (replace with your own):

```text
d-2c214ac919e84170b21855cc129b4a5f
```

Template Body:

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

## With Mail Helper Class

```java
import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
Mail mail = new Mail();
mail.setFrom(new Email("teste@example.com"));
mail.setTemplateId("d-2c214ac919e84170b21855cc129b4a5f");

Personalization personalization = new Personalization();
personalization.addDynamicTemplateData("name", "Example User");
personalization.addDynamicTemplateData("city", "Denver");
personalization.addTo(new Email("test@example.com"));
mail.addPersonalization(personalization);

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}
```

## Without Mail Helper Class

```java
import com.sendgrid.*;
import java.io.IOException;

public class Example {
public static void main(String[] args) throws IOException {
try {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{
\"from\": {\"email\": \"test@example.com\"},
\"personalizations\":
[{
\"to\": [{\"email\": \"test@example.com\"}],
\"dynamic_template_data\": {\"name\": \"Example User\", \"city\": \"Denver\"}
}],
\"template_id\": \"d-2c214ac919e84170b21855cc129b4a5f\"}");
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
}
```

<a name="legacy-templates"></a>
# Legacy Templates

For this example, we assume you have created a [legacy 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
13b8f94f-bcae-4ec6-b752-70d6cb59f932
```
Expand All @@ -27,19 +124,19 @@ Template Body:

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

Expand Down Expand Up @@ -90,7 +187,20 @@ public class Example {
Request request = new Request();
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"test@example.com\"}],\"substitutions\":{\"-name-\":\"Example User\",\"-city-\":\"Denver\"},\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"test@example.com\"},\"content\":[{\"type\":\"text/html\",\"value\": \"I'm replacing the <strong>body tag</strong>\"}],\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}");
request.setBody("{
\"personalizations\":
[{
\"to\": [{\"email\": \"test@example.com\"}],
\"substitutions\": {\"-name-\": \"Example User\", \"-city-\": \"Denver\"},
\"subject\": \"Hello World from the SendGrid Java Library!\"
}],
\"from\": {\"email\": \"test@example.com\"},
\"content\":
[{
\"type\": \"text/html\",
\"value\": \"I'm replacing the <strong>body tag</strong>\"
}]
,\"template_id\": \"13b8f94f-bcae-4ec6-b752-70d6cb59f932\"}");
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
Expand All @@ -102,14 +212,14 @@ public class Example {
}
```

<a name="domain_whitelabel"></a>
<a name="domain-whitelabel"></a>
# How to Setup a Domain Whitelabel

You can find documentation for how to setup a domain whitelabel via the UI [here](https://sendgrid.com/docs/Classroom/Basics/Whitelabel/setup_domain_whitelabel.html) and via API [here](https://github.com/sendgrid/sendgrid-java/blob/master/USAGE.md#whitelabel).

Find more information about all of SendGrid's whitelabeling related documentation [here](https://sendgrid.com/docs/Classroom/Basics/Whitelabel/index.html).

<a name="email_stats"></a>
<a name="email-stats"></a>
# How to View Email Statistics

You can find documentation for how to view your email statistics via the UI [here](https://app.sendgrid.com/statistics) and via API [here](https://github.com/sendgrid/sendgrid-java/blob/master/USAGE.md#stats).
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN chmod +x ./install.sh && \
WORKDIR /root/sources
RUN git clone https://github.com/sendgrid/sendgrid-java.git
WORKDIR /root
RUN ln -s /root/sources/sendgrid-java/sendgrid
RUN ln -s /root/sources/sendgrid-java

COPY entrypoint.sh entrypoint.sh
RUN chmod +x entrypoint.sh
Expand Down
5 changes: 4 additions & 1 deletion docker/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ You can mount repositories in the `/mnt/sendgrid-java` and `/mnt/java-http-clien

<a name="Testing"></a>
# Testing
Testing is easy! Run the container, `cd sendgrid`, and run `./gradlew test`.
Testing is easy!
1. Run the container: `docker run -it sendgrid/sendgrid-java`
2. `cd sendgrid-java`
3. run `./gradlew test`

<a name="about"></a>
# About
Expand Down
44 changes: 42 additions & 2 deletions examples/helpers/mail/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ public static Mail buildKitchenSink() throws IOException {
return mail;
}

// API V3 Dynamic Template implementation
public static Mail buildDynamicTemplate() throws IOException {
Mail mail = new Mail();

Email fromEmail = new Email();
fromEmail.setName("Example User");
fromEmail.setEmail("test@example.com");
mail.setFrom(fromEmail);

mail.setTemplateId("d-c6dcf1f72bdd4beeb15a9aa6c72fcd2c");

Personalization personalization = new Personalization();
personalization.addDynamicTemplateData("name", "Example User");
personalization.addDynamicTemplateData("city", "Denver");
personalization.addTo(new Email("test@example.com"));
mail.addPersonalization(personalization);

return mail;
}

// Minimum required to send an email
public static Mail buildHelloEmail() throws IOException {
Email from = new Email("test@example.com");
Expand Down Expand Up @@ -261,8 +281,28 @@ public static void kitchenSinkExample() throws IOException {
}
}

public static void dynamicTemplateExample() throws IOException {
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
sg.addRequestHeader("X-Mock", "true");

Request request = new Request();
Mail dynamicTemplate = buildDynamicTemplate();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(dynamicTemplate.build());
Response response = sg.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}

public static void main(String[] args) throws IOException {
baselineExample();
kitchenSinkExample();
// baselineExample();
// kitchenSinkExample();
dynamicTemplateExample();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class Personalization {
@JsonProperty("headers") private Map<String,String> headers;
@JsonProperty("substitutions") private Map<String,String> substitutions;
@JsonProperty("custom_args") private Map<String,String> customArgs;
@JsonProperty("dynamic_template_data") private Map<String,Object> dynamicTemplateData;
@JsonProperty("send_at") private long sendAt;

@JsonProperty("to")
public List<Email> getTos() {
if(tos == null)
Expand Down Expand Up @@ -144,6 +145,19 @@ public void setSendAt(long sendAt) {
this.sendAt = sendAt;
}

@JsonProperty("dynamic_template_data")
public Map<String,Object> getDynamicTemplateData() {
return dynamicTemplateData == null
? Collections.<String,Object>emptyMap() : dynamicTemplateData;
}

public void addDynamicTemplateData(String key, Object value) {
if (dynamicTemplateData == null) {
dynamicTemplateData = new HashMap<String,Object>();
}
dynamicTemplateData.put(key, value);
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
Loading