Skip to content

Commit a2c71ef

Browse files
author
Bilal Boussayoud
authored
feat: allow personalization of the From name and email for each email recipient (#706)
* feat: allow personalization of the From name and email for email recipients
1 parent bb9c0ea commit a2c71ef

6 files changed

Lines changed: 87 additions & 6 deletions

File tree

examples/helpers/mail/Example.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,26 @@ public static Mail buildKitchenSink() {
5757
to2.setName("Example User");
5858
to2.setEmail("test1@example.com");
5959
personalization2.addTo(to2);
60+
Email fromEmail2 = new Email();
61+
fromEmail2.setName("Example User");
62+
fromEmail2.setEmail("test2@example.com");
63+
personalization2.setFrom(fromEmail2);
6064
to2.setName("Example User");
61-
to2.setEmail("test2@example.com");
65+
to2.setEmail("test3@example.com");
6266
personalization2.addTo(to2);
6367
Email cc2 = new Email();
6468
cc2.setName("Example User");
65-
cc2.setEmail("test3@example.com");
69+
cc2.setEmail("test4@example.com");
6670
personalization2.addCc(cc2);
6771
cc2.setName("Example User");
68-
cc2.setEmail("test4@example.com");
72+
cc2.setEmail("test5@example.com");
6973
personalization2.addCc(cc2);
7074
Email bcc2 = new Email();
7175
bcc2.setName("Example User");
72-
bcc2.setEmail("test5@example.com");
76+
bcc2.setEmail("test6@example.com");
7377
personalization2.addBcc(bcc2);
7478
bcc2.setName("Example User");
75-
bcc2.setEmail("test6@example.com");
79+
bcc2.setEmail("test7@example.com");
7680
personalization2.addBcc(bcc2);
7781
personalization2.setSubject("Hello World from the Personalized Twilio SendGrid Java Library");
7882
personalization2.addHeader("X-Test", "test");

examples/helpers/mail/MultipleEmailsMultipleRecipients.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static void main(String[] args) throws IOException {
2929

3030
final Personalization personalization2 = new Personalization();
3131
personalization2.addTo(new Email("test2@example.com", "Example User2"));
32+
personalization2.setFrom(new Email("test3@example.com", "Example User3"));
3233
personalization2.addDynamicTemplateData("name", "Example User2");
3334
personalization2.addDynamicTemplateData("city", "San Francisco");
3435
mail.addPersonalization(personalization2);

src/main/java/com/sendgrid/helpers/mail/objects/Personalization.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public class Personalization {
1515
@JsonProperty("to")
1616
private List<Email> tos;
1717

18+
@JsonProperty("from")
19+
private Email from;
20+
1821
@JsonProperty("cc")
1922
private List<Email> ccs;
2023

@@ -59,6 +62,15 @@ public void addTo(Email email) {
5962
}
6063
}
6164

65+
@JsonProperty("from")
66+
public Email getFrom() {
67+
return from;
68+
}
69+
70+
public void setFrom(Email email) {
71+
this.from = email;
72+
}
73+
6274
@JsonProperty("cc")
6375
public List<Email> getCcs() {
6476
if (ccs == null) {
@@ -260,6 +272,13 @@ public boolean equals(Object obj) {
260272
} else if (!tos.equals(other.tos)) {
261273
return false;
262274
}
275+
if (from == null) {
276+
if (other.from != null) {
277+
return false;
278+
}
279+
} else if (!from.equals(other.from)) {
280+
return false;
281+
}
263282
return true;
264283
}
265284
}

src/test/java/com/sendgrid/helpers/MailTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ public void testKitchenSink() throws IOException {
7878
to2.setName("Example User");
7979
to2.setEmail("test@example.com");
8080
personalization2.addTo(to2);
81+
Email fromEmail2 = new Email();
82+
fromEmail2.setName("Example Sender");
83+
fromEmail2.setEmail("sender@example.com");
84+
personalization2.setFrom(fromEmail2);
8185
Email cc2 = new Email();
8286
cc2.setName("Example User");
8387
cc2.setEmail("test@example.com");
@@ -110,6 +114,10 @@ public void testKitchenSink() throws IOException {
110114
to3.setName("Example User");
111115
to3.setEmail("test@example.com");
112116
personalization3.addTo(to3);
117+
Email fromEmail3 = new Email();
118+
fromEmail3.setName("Example Sender2");
119+
fromEmail3.setEmail("sender2@example.com");
120+
personalization3.setFrom(fromEmail3);
113121
Email cc3 = new Email();
114122
cc3.setName("Example User");
115123
cc3.setEmail("test@example.com");
@@ -247,7 +255,7 @@ public void testKitchenSink() throws IOException {
247255
replyTo.setEmail("test@example.com");
248256
mail.setReplyTo(replyTo);
249257

250-
Assert.assertEquals(mail.build(), "{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Hello World from the SendGrid Java Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"dynamic_template_data\":{\"city\":\"Denver\",\"items\":[{\"price\":\"$ 59.95\",\"text\":\"New Line Sneakers\"},{\"text\":\"Old Line Sneakers\"}],\"name\":\"Example User\"},\"send_at\":1443636843}],\"content\":[{\"type\":\"text/plain\",\"value\":\"some text here\"},{\"type\":\"text/html\",\"value\":\"<html><body>some text here</body></html>\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"sections\":{\"%section1%\":\"Substitution Text for Section 1\",\"%section2%\":\"Substitution Text for Section 2\"},\"headers\":{\"X-Test1\":\"1\",\"X-Test2\":\"2\"},\"categories\":[\"May\",\"2016\"],\"custom_args\":{\"campaign\":\"welcome\",\"weekday\":\"morning\"},\"send_at\":1443636842,\"asm\":{\"group_id\":99,\"groups_to_display\":[4,5,6,7,8]},\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"test@example.com\"},\"bypass_list_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Footer Text\",\"html\":\"<html><body>Footer Text</body></html>\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://spamcatcher.sendgrid.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"<html><body>html to insert into the text/html portion of the message</body></html>\",\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some name\",\"utm_medium\":\"some medium\"}},\"reply_to\":{\"name\":\"Example User\",\"email\":\"test@example.com\"}}");
258+
Assert.assertEquals(mail.build(), "{\"from\":{\"name\":\"Example User\",\"email\":\"test@example.com\"},\"subject\":\"Hello World from the SendGrid Java Library\",\"personalizations\":[{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"from\":{\"name\":\"Example Sender\",\"email\":\"sender@example.com\"},\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"substitutions\":{\"%city%\":\"Denver\",\"%name%\":\"Example User\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"send_at\":1443636843},{\"to\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"from\":{\"name\":\"Example Sender2\",\"email\":\"sender2@example.com\"},\"cc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"bcc\":[{\"name\":\"Example User\",\"email\":\"test@example.com\"},{\"name\":\"Example User\",\"email\":\"test@example.com\"}],\"subject\":\"Hello World from the Personalized SendGrid Java Library\",\"headers\":{\"X-Mock\":\"true\",\"X-Test\":\"test\"},\"custom_args\":{\"type\":\"marketing\",\"user_id\":\"343\"},\"dynamic_template_data\":{\"city\":\"Denver\",\"items\":[{\"price\":\"$ 59.95\",\"text\":\"New Line Sneakers\"},{\"text\":\"Old Line Sneakers\"}],\"name\":\"Example User\"},\"send_at\":1443636843}],\"content\":[{\"type\":\"text/plain\",\"value\":\"some text here\"},{\"type\":\"text/html\",\"value\":\"<html><body>some text here</body></html>\"}],\"attachments\":[{\"content\":\"TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12\",\"type\":\"application/pdf\",\"filename\":\"balance_001.pdf\",\"disposition\":\"attachment\",\"content_id\":\"Balance Sheet\"},{\"content\":\"BwdW\",\"type\":\"image/png\",\"filename\":\"banner.png\",\"disposition\":\"inline\",\"content_id\":\"Banner\"}],\"template_id\":\"13b8f94f-bcae-4ec6-b752-70d6cb59f932\",\"sections\":{\"%section1%\":\"Substitution Text for Section 1\",\"%section2%\":\"Substitution Text for Section 2\"},\"headers\":{\"X-Test1\":\"1\",\"X-Test2\":\"2\"},\"categories\":[\"May\",\"2016\"],\"custom_args\":{\"campaign\":\"welcome\",\"weekday\":\"morning\"},\"send_at\":1443636842,\"asm\":{\"group_id\":99,\"groups_to_display\":[4,5,6,7,8]},\"ip_pool_name\":\"23\",\"mail_settings\":{\"bcc\":{\"enable\":true,\"email\":\"test@example.com\"},\"bypass_list_management\":{\"enable\":true},\"footer\":{\"enable\":true,\"text\":\"Footer Text\",\"html\":\"<html><body>Footer Text</body></html>\"},\"sandbox_mode\":{\"enable\":true},\"spam_check\":{\"enable\":true,\"threshold\":1,\"post_to_url\":\"https://spamcatcher.sendgrid.com\"}},\"tracking_settings\":{\"click_tracking\":{\"enable\":true,\"enable_text\":false},\"open_tracking\":{\"enable\":true,\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"subscription_tracking\":{\"enable\":true,\"text\":\"text to insert into the text/plain portion of the message\",\"html\":\"<html><body>html to insert into the text/html portion of the message</body></html>\",\"substitution_tag\":\"Optional tag to replace with the open image in the body of the message\"},\"ganalytics\":{\"enable\":true,\"utm_source\":\"some source\",\"utm_term\":\"some term\",\"utm_content\":\"some content\",\"utm_campaign\":\"some name\",\"utm_medium\":\"some medium\"}},\"reply_to\":{\"name\":\"Example User\",\"email\":\"test@example.com\"}}");
251259
}
252260

253261
@Test

use-cases/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Please [open an issue](https://github.com/sendgrid/sendgrid-ruby/issues) or [mak
55
# Email Use Cases
66
* [Transactional Templates](transactional-templates.md)
77
* [Legacy Templates](legacy-templates.md)
8+
* [Personalizations](personalizations.md)
89

910
# Twilio Use Cases
1011
* [Twilio Setup](twilio-setup.md)

use-cases/personalizations.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Personalizations
2+
3+
This example goes over how to send multiple emails using personalizations with the helper class.
4+
5+
A similar example can be found in `examples/helpers/mail/MultipleEmailsMultipleRecipients.java`, and further documentation can be found [here](https://docs.sendgrid.com/for-developers/sending-email/personalizations).
6+
7+
```java
8+
import com.sendgrid.*;
9+
import java.io.IOException;
10+
11+
public class Example {
12+
public static void main(String[] args) {
13+
final Mail mail = new Mail();
14+
15+
// Note that the domain of the from addresses should be the same
16+
mail.setFrom(new Email("test@example.com", "Example User"));
17+
mail.setSubject("I'm the original subject");
18+
mail.addContent(new Content("text/plain", "and this is some content"));
19+
mail.addContent(new Content("text/html", "<strong>and this is some content</strong>"));
20+
21+
final Personalization personalization1 = new Personalization();
22+
personalization1.addTo(new Email("test1@example.com", "Example User1"));
23+
personalization1.addCc(new Email("test2@example.com", "Example User2"));
24+
mail.addPersonalization(personalization1);
25+
26+
final Personalization personalization2 = new Personalization();
27+
personalization2.addTo(new Email("test3@example.com", "Example User3"));
28+
personalization2.setFrom(new Email("test4@example.com", "Example User4"));
29+
personalization2.setSubject(new Subject("I'm the personalized subject"));
30+
personalization2.addBcc(new Email("test5@example.com", "Example User5"));
31+
mail.addPersonalization(personalization2);
32+
33+
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
34+
Request request = new Request();
35+
try {
36+
request.setMethod(Method.POST);
37+
request.setEndpoint("mail/send");
38+
request.setBody(mail.build());
39+
Response response = sg.api(request);
40+
System.out.println(response.getStatusCode());
41+
System.out.println(response.getBody());
42+
System.out.println(response.getHeaders());
43+
} catch (IOException ex) {
44+
throw ex;
45+
}
46+
}
47+
}
48+
```

0 commit comments

Comments
 (0)