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

feat: allow personalization of the From name & email for each email recipient #1312

Merged
merged 2 commits into from
Oct 18, 2021
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
1 change: 1 addition & 0 deletions docs/use-cases/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This documentation provides examples for specific Twilio SendGrid v3 API use cas
* [Send a Single Email to a Single Recipient](single-email-single-recipient.md)
* [Send a Single Email to Multiple Recipients](single-email-multiple-recipients.md)
* [Send Multiple Emails to Multiple Recipients](multiple-emails-multiple-recipients.md)
* [Send Multiple Emails with Personalizations](multiple-emails-personalizations.md)
* [CC, BCC and Reply To](cc-bcc-reply-to.md)
* [Flexible Email Address Fields](flexible-address-fields.md)
* [Handling Success/Failure/Errors](success-failure-errors.md)
Expand Down
39 changes: 39 additions & 0 deletions docs/use-cases/multiple-emails-personalizations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Send Multiple Emails with Personalizations

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.

Refer to [the Sendgrid documentation](https://docs.sendgrid.com/for-developers/sending-email/personalizations) for more details about personalizations.
```js
const sgMail = require('@sendgrid/mail');
const sgHelpers = require('@sendgrid/helpers');
const Personalization = sgHelpers.classes.Personalization;

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
personalizations: []
};

const personalization1 = new Personalization();
personalization1.setTo(['[email protected]', '[email protected]']);
personalization1.setCc('[email protected]');
msg.personalizations.push(personalization1);

const personalization2 = new Personalization();
personalization2.setTo(['[email protected]', '[email protected]', '[email protected]']);
personalization2.setFrom('[email protected]');
personalization2.setCc('[email protected]');
msg.personalizations.push(personalization2);

const personalization3 = new Personalization();
personalization3.setTo('[email protected]');
personalization3.setFrom('[email protected]');
personalization3.setCc('[email protected]');
personalization3.setSubject('Greetings world');
msg.personalizations.push(personalization3);

sgMail.send(msg);
```
7 changes: 7 additions & 0 deletions packages/helpers/classes/personalization.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EmailData, EmailJSON } from "./email-address";

export interface PersonalizationData {
to: EmailData | EmailData[],
from?: EmailData,
cc?: EmailData | EmailData[],
bcc?: EmailData | EmailData[],
subject?: string;
Expand All @@ -14,6 +15,7 @@ export interface PersonalizationData {

export interface PersonalizationJSON {
to: EmailJSON | EmailJSON[];
from?: EmailJSON;
cc?: EmailJSON[];
bcc?: EmailJSON[];
headers?: { [key: string]: string; };
Expand Down Expand Up @@ -44,6 +46,11 @@ export default class Personalization {
*/
setTo(to: EmailData | EmailData[]): void;

/**
* Set from
*/
setFrom(from: EmailData): void;

/**
* Add a single to
*/
Expand Down
18 changes: 16 additions & 2 deletions packages/helpers/classes/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ class Personalization {

//Extract properties from data
const {
to, cc, bcc, subject, headers, customArgs, sendAt,
to, from, cc, bcc, subject, headers, customArgs, sendAt,
substitutions, substitutionWrappers, dynamicTemplateData,
} = data;

//Set data
this.setTo(to);
this.setFrom(from);
this.setCc(cc);
this.setBcc(bcc);
this.setSubject(subject);
Expand Down Expand Up @@ -109,6 +110,16 @@ class Personalization {
this.to = EmailAddress.create(to);
}

/**
* Set from
* */
setFrom(from) {
if (typeof from === 'undefined') {
return;
}
this.from = EmailAddress.create(from);
}

/**
* Add a single to
*/
Expand Down Expand Up @@ -309,7 +320,7 @@ class Personalization {

//Get data from self
const {
to, cc, bcc, subject, headers, customArgs, sendAt,
to, from, cc, bcc, subject, headers, customArgs, sendAt,
substitutions, substitutionWrappers, dynamicTemplateData,
} = this;

Expand Down Expand Up @@ -347,6 +358,9 @@ class Personalization {
if (typeof sendAt !== 'undefined') {
json.sendAt = sendAt;
}
if (typeof from !== 'undefined') {
json.from = from;
}

//Return as snake cased object
return toSnakeCase(json, ['substitutions', 'dynamicTemplateData', 'customArgs', 'headers']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- setSubject() - set-subject.spec.js
- setSendAt() - set-send-at.spec.js
- setTo() - set-to.spec.js
- setFrom() - set-from.spec.js
- addTo() - add-to.spec.js
- setCc() - set-cc.spec.js
- addCc() - add-cc.spec.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Personalization', function() {
//Data
const data = {
to: '[email protected]',
from: '[email protected]',
cc: ['[email protected]', '[email protected]'],
bcc: ['[email protected]', '[email protected]'],
subject: 'Test',
Expand All @@ -47,6 +48,7 @@ describe('Personalization', function() {
it('should have set all properties', () => {
p.fromData(data);
expect(p.to[0].email).to.equal('[email protected]');
expect(p.from.email).to.equal('[email protected]');
expect(p.cc[0].email).to.equal('[email protected]');
expect(p.cc[1].email).to.equal('[email protected]');
expect(p.bcc[0].email).to.equal('[email protected]');
Expand Down
45 changes: 45 additions & 0 deletions packages/helpers/classes/personalization_specs/set-from.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

/**
* Dependencies
*/
const Personalization = require('../personalization');
const EmailAddress = require('../email-address');

/**
* Tests
*/
describe('Personalization', function() {

//Create new personalization before each test
let p;
beforeEach(function() {
p = new Personalization();
});

//Set from
describe('setFrom()', function() {
it('should accept string values', function() {
p.setFrom('[email protected]');
expect(p.from).to.be.an.instanceof(EmailAddress);
expect(p.from.email).to.equal('[email protected]');
});
it('should properly update from value', function() {
p.setFrom('[email protected]');
p.setFrom('[email protected]');
p.setFrom('[email protected]');
p.setFrom('[email protected]');
expect(p.from.email).to.equal('[email protected]');
});
it('should accept no input', function() {
expect(function() {
p.setFrom();
}).not.to.throw(Error);
});
it('should not overwrite value with no input', function() {
p.setFrom('[email protected]');
p.setFrom();
expect(p.from.email).to.equal('[email protected]');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ describe('Personalization', function() {
expect(json.to[0]).to.be.an.instanceof(EmailAddress);
expect(json.to[0].email).to.equal('[email protected]');
});
it('should set the from field', function() {
p.setFrom('[email protected]');
const json = p.toJSON();
expect(json).to.have.property('from');
expect(json.from).to.be.an.instanceof(EmailAddress);
expect(json.from.email).to.equal('[email protected]');
});
it('should set the cc field', function() {
p.setCc('[email protected]');
const json = p.toJSON();
Expand Down
1 change: 1 addition & 0 deletions test/typescript/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ new helpers.classes.EmailAddress({ name: "Some One", email: "[email protected]

new helpers.classes.Personalization({
to: "[email protected]",
from: "[email protected]",
subject: "Hello Some One",
dynamicTemplateData: {
translations: {
Expand Down