Skip to content

Commit a068f9d

Browse files
author
Bilal Boussayoud
authored
feat: allow personalization of the From name & email for each email recipient (#1312)
* feat: allow personalization of the From name & email for each email recipient
1 parent a3307fd commit a068f9d

File tree

9 files changed

+119
-2
lines changed

9 files changed

+119
-2
lines changed

docs/use-cases/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This documentation provides examples for specific Twilio SendGrid v3 API use cas
44
* [Send a Single Email to a Single Recipient](single-email-single-recipient.md)
55
* [Send a Single Email to Multiple Recipients](single-email-multiple-recipients.md)
66
* [Send Multiple Emails to Multiple Recipients](multiple-emails-multiple-recipients.md)
7+
* [Send Multiple Emails with Personalizations](multiple-emails-personalizations.md)
78
* [CC, BCC and Reply To](cc-bcc-reply-to.md)
89
* [Flexible Email Address Fields](flexible-address-fields.md)
910
* [Handling Success/Failure/Errors](success-failure-errors.md)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Send Multiple Emails with Personalizations
2+
3+
Personalizations are an array of objects, each representing a separate email, that allow you to customize the metadata of each email sent within a request. The below example shows how multiple emails, each with varying metadata, are sent with personalizations.
4+
5+
Refer to [the Sendgrid documentation](https://docs.sendgrid.com/for-developers/sending-email/personalizations) for more details about personalizations.
6+
```js
7+
const sgMail = require('@sendgrid/mail');
8+
const sgHelpers = require('@sendgrid/helpers');
9+
const Personalization = sgHelpers.classes.Personalization;
10+
11+
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
12+
const msg = {
13+
14+
subject: 'Hello world',
15+
text: 'Hello plain world!',
16+
html: '<p>Hello HTML world!</p>',
17+
personalizations: []
18+
};
19+
20+
const personalization1 = new Personalization();
21+
personalization1.setTo(['[email protected]', '[email protected]']);
22+
personalization1.setCc('[email protected]');
23+
msg.personalizations.push(personalization1);
24+
25+
const personalization2 = new Personalization();
26+
personalization2.setTo(['[email protected]', '[email protected]', '[email protected]']);
27+
personalization2.setFrom('[email protected]');
28+
personalization2.setCc('[email protected]');
29+
msg.personalizations.push(personalization2);
30+
31+
const personalization3 = new Personalization();
32+
personalization3.setTo('[email protected]');
33+
personalization3.setFrom('[email protected]');
34+
personalization3.setCc('[email protected]');
35+
personalization3.setSubject('Greetings world');
36+
msg.personalizations.push(personalization3);
37+
38+
sgMail.send(msg);
39+
```

packages/helpers/classes/personalization.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EmailData, EmailJSON } from "./email-address";
22

33
export interface PersonalizationData {
44
to: EmailData | EmailData[],
5+
from?: EmailData,
56
cc?: EmailData | EmailData[],
67
bcc?: EmailData | EmailData[],
78
subject?: string;
@@ -14,6 +15,7 @@ export interface PersonalizationData {
1415

1516
export interface PersonalizationJSON {
1617
to: EmailJSON | EmailJSON[];
18+
from?: EmailJSON;
1719
cc?: EmailJSON[];
1820
bcc?: EmailJSON[];
1921
headers?: { [key: string]: string; };
@@ -44,6 +46,11 @@ export default class Personalization {
4446
*/
4547
setTo(to: EmailData | EmailData[]): void;
4648

49+
/**
50+
* Set from
51+
*/
52+
setFrom(from: EmailData): void;
53+
4754
/**
4855
* Add a single to
4956
*/

packages/helpers/classes/personalization.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ class Personalization {
5353

5454
//Extract properties from data
5555
const {
56-
to, cc, bcc, subject, headers, customArgs, sendAt,
56+
to, from, cc, bcc, subject, headers, customArgs, sendAt,
5757
substitutions, substitutionWrappers, dynamicTemplateData,
5858
} = data;
5959

6060
//Set data
6161
this.setTo(to);
62+
this.setFrom(from);
6263
this.setCc(cc);
6364
this.setBcc(bcc);
6465
this.setSubject(subject);
@@ -109,6 +110,16 @@ class Personalization {
109110
this.to = EmailAddress.create(to);
110111
}
111112

113+
/**
114+
* Set from
115+
* */
116+
setFrom(from) {
117+
if (typeof from === 'undefined') {
118+
return;
119+
}
120+
this.from = EmailAddress.create(from);
121+
}
122+
112123
/**
113124
* Add a single to
114125
*/
@@ -309,7 +320,7 @@ class Personalization {
309320

310321
//Get data from self
311322
const {
312-
to, cc, bcc, subject, headers, customArgs, sendAt,
323+
to, from, cc, bcc, subject, headers, customArgs, sendAt,
313324
substitutions, substitutionWrappers, dynamicTemplateData,
314325
} = this;
315326

@@ -347,6 +358,9 @@ class Personalization {
347358
if (typeof sendAt !== 'undefined') {
348359
json.sendAt = sendAt;
349360
}
361+
if (typeof from !== 'undefined') {
362+
json.from = from;
363+
}
350364

351365
//Return as snake cased object
352366
return toSnakeCase(json, ['substitutions', 'dynamicTemplateData', 'customArgs', 'headers']);

packages/helpers/classes/personalization_specs/PERSONALIZATION-SPECS-README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- setSubject() - set-subject.spec.js
44
- setSendAt() - set-send-at.spec.js
55
- setTo() - set-to.spec.js
6+
- setFrom() - set-from.spec.js
67
- addTo() - add-to.spec.js
78
- setCc() - set-cc.spec.js
89
- addCc() - add-cc.spec.js

packages/helpers/classes/personalization_specs/from-data.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('Personalization', function() {
2323
//Data
2424
const data = {
2525
26+
2627
2728
2829
subject: 'Test',
@@ -47,6 +48,7 @@ describe('Personalization', function() {
4748
it('should have set all properties', () => {
4849
p.fromData(data);
4950
expect(p.to[0].email).to.equal('[email protected]');
51+
expect(p.from.email).to.equal('[email protected]');
5052
expect(p.cc[0].email).to.equal('[email protected]');
5153
expect(p.cc[1].email).to.equal('[email protected]');
5254
expect(p.bcc[0].email).to.equal('[email protected]');
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
/**
4+
* Dependencies
5+
*/
6+
const Personalization = require('../personalization');
7+
const EmailAddress = require('../email-address');
8+
9+
/**
10+
* Tests
11+
*/
12+
describe('Personalization', function() {
13+
14+
//Create new personalization before each test
15+
let p;
16+
beforeEach(function() {
17+
p = new Personalization();
18+
});
19+
20+
//Set from
21+
describe('setFrom()', function() {
22+
it('should accept string values', function() {
23+
p.setFrom('[email protected]');
24+
expect(p.from).to.be.an.instanceof(EmailAddress);
25+
expect(p.from.email).to.equal('[email protected]');
26+
});
27+
it('should properly update from value', function() {
28+
p.setFrom('[email protected]');
29+
p.setFrom('[email protected]');
30+
p.setFrom('[email protected]');
31+
p.setFrom('[email protected]');
32+
expect(p.from.email).to.equal('[email protected]');
33+
});
34+
it('should accept no input', function() {
35+
expect(function() {
36+
p.setFrom();
37+
}).not.to.throw(Error);
38+
});
39+
it('should not overwrite value with no input', function() {
40+
p.setFrom('[email protected]');
41+
p.setFrom();
42+
expect(p.from.email).to.equal('[email protected]');
43+
});
44+
});
45+
});

packages/helpers/classes/personalization_specs/to-json.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ describe('Personalization', function() {
3131
expect(json.to[0]).to.be.an.instanceof(EmailAddress);
3232
expect(json.to[0].email).to.equal('[email protected]');
3333
});
34+
it('should set the from field', function() {
35+
p.setFrom('[email protected]');
36+
const json = p.toJSON();
37+
expect(json).to.have.property('from');
38+
expect(json.from).to.be.an.instanceof(EmailAddress);
39+
expect(json.from.email).to.equal('[email protected]');
40+
});
3441
it('should set the cc field', function() {
3542
p.setCc('[email protected]');
3643
const json = p.toJSON();

test/typescript/helpers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ new helpers.classes.EmailAddress({ name: "Some One", email: "[email protected]
1010

1111
new helpers.classes.Personalization({
1212
13+
1314
subject: "Hello Some One",
1415
dynamicTemplateData: {
1516
translations: {

0 commit comments

Comments
 (0)