daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2018 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const sendgrid = require('sendgrid');
  2. const helper = sendgrid.mail;
  3. const keys = require('../config/keys');
  4.  
  5. class Mailer extends helper.Mail {
  6.     constructor({ subject, recipients }, content) {
  7.         super();
  8.  
  9.         this.sgApi = sendgrid(keys.sendGridKey);
  10.         this.from_email = new helper.Email('no-reply@emaily.com');
  11.         this.subject = subject;
  12.         this.body = new helper.Content('text/html', content);
  13.         this.recipients = this.formatAddresses(recipients);
  14.  
  15.         this.addContent(this.body);
  16.         this.addClickTracking();
  17.         this.addRecipients();
  18.     }
  19.  
  20.     formatAddresses(recipients) {
  21.         return recipients.map(({ email }) => {
  22.             return new helper.Email(email);
  23.         });
  24.     }
  25.  
  26.     addClickTracking() {
  27.         const trackingSettings = new helper.TrackingSettings();
  28.         const clickTracking = new helper.ClickTracking(true, true);
  29.  
  30.         trackingSettings.setClickTracking(clickTracking);
  31.         this.addTrackingSettings(trackingSettings);
  32.     }
  33.  
  34.     addRecipients() {
  35.         const personalize = new helper.Personalization();
  36.  
  37.         this.recipients.forEach(recipient => {
  38.             personalize.addTo(recipient);
  39.         });
  40.         this.addPersonalization(personalize);
  41.     }
  42.  
  43.     async send() {
  44.         const request = this.sgApi.emptyRequest({
  45.             method: 'POST',
  46.             path: '/v3/mail/send',
  47.             body: this.toJSON()
  48.         });
  49.  
  50.         const response = await this.sgApi.API(request);
  51.         return response;
  52.     }
  53. }
  54.  
  55. module.exports = Mailer;
RAW Paste Data
Top