-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
email template ;; mailer --- all the
mail.(....)
is sendgrid
api
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 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,71 @@ | ||
const passport = require('sendgrid'); | ||
const helper = sendgrid.mail; // `helper` name is used thruout sendgrid documentation | ||
|
||
const keys = require('../config/keys'); | ||
|
||
|
||
class Mailer extends helper.Mail { | ||
|
||
// can be any obj with {subject, recipients} | ||
// used with Survey db model to pull these 2 properties from it | ||
constructor({subject, recipients}, content){ | ||
super(); | ||
|
||
this.sgAPI = sendgrid(keys.sendGridKey); | ||
|
||
this.from_email = new helper.Email('[email protected]'); | ||
this.subject subject; | ||
this.body = new helper.Content('text/html', content); | ||
this.recipients = this.formatAddresses(recipients); | ||
|
||
this.addContent(this.body); | ||
|
||
this.addClickTracking(); | ||
this.addRecipients(); | ||
} | ||
|
||
formatAddresses(recipients){ | ||
|
||
return recipients.map({email}) => { | ||
return new helper.Email(email); | ||
} | ||
} | ||
|
||
addClickTracking(){ | ||
|
||
const trackingSettings = new helper.TrackingSettings(); | ||
const clickTracking = new helper.ClickTracking(true, true); | ||
|
||
trackingSettings.setClickTracking(clicktracking); | ||
this.addTrackingSettings(trackingSettings); | ||
|
||
} | ||
|
||
addRecipients(){ | ||
|
||
const personalize = this.recipients.forEach(recipient => { | ||
|
||
personalize.addTo(recipient); | ||
|
||
}); | ||
|
||
this.addPersonalization(personalize); | ||
|
||
} | ||
|
||
async send(){ | ||
|
||
const request = sgAPI.emptyRequest({ | ||
method: 'POST', | ||
path: 'v3/mail/send', | ||
body: this.toJSON() | ||
}); | ||
|
||
const response = await this.sgAPI.API(request); | ||
return response; | ||
|
||
} | ||
} | ||
|
||
|
||
module.exports = Mailer; |
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,5 @@ | ||
module.exports = (survey) => { | ||
|
||
return ( "<div> Hello " + survey.body + " </div>" ); | ||
|
||
}; |