Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow personalization of the From name & email for each email r…
…ecipient (#1312) * feat: allow personalization of the From name & email for each email recipient
- Loading branch information
Bilal Boussayoud
committed
Oct 18, 2021
1 parent
a3307fd
commit a068f9d
Showing
9 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'sender1@example.org', | ||
subject: 'Hello world', | ||
text: 'Hello plain world!', | ||
html: '<p>Hello HTML world!</p>', | ||
personalizations: [] | ||
}; | ||
|
||
const personalization1 = new Personalization(); | ||
personalization1.setTo(['recipient2@example.org', 'recipient3@example.org']); | ||
personalization1.setCc('recipient4@example.org'); | ||
msg.personalizations.push(personalization1); | ||
|
||
const personalization2 = new Personalization(); | ||
personalization2.setTo(['recipient5@example.org', 'recipient6@example.org', 'recipient7@example.org']); | ||
personalization2.setFrom('sender2@example.org'); | ||
personalization2.setCc('recipient8@example.org'); | ||
msg.personalizations.push(personalization2); | ||
|
||
const personalization3 = new Personalization(); | ||
personalization3.setTo('recipient9@example.org'); | ||
personalization3.setFrom('sender3@example.org'); | ||
personalization3.setCc('recipient10@example.org'); | ||
personalization3.setSubject('Greetings world'); | ||
msg.personalizations.push(personalization3); | ||
|
||
sgMail.send(msg); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/helpers/classes/personalization_specs/set-from.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('test@example.org'); | ||
expect(p.from).to.be.an.instanceof(EmailAddress); | ||
expect(p.from.email).to.equal('test@example.org'); | ||
}); | ||
it('should properly update from value', function() { | ||
p.setFrom('test1@example.com'); | ||
p.setFrom('test2@example.com'); | ||
p.setFrom('test3@example.com'); | ||
p.setFrom('test4@example.com'); | ||
expect(p.from.email).to.equal('test4@example.com'); | ||
}); | ||
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('test@example.org'); | ||
p.setFrom(); | ||
expect(p.from.email).to.equal('test@example.org'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters