Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  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, (error, response) => {
  51. if (error) {
  52. console.log("Error response received");
  53. }
  54. console.log(response.statusCode);
  55. console.log(response.body);
  56. console.log(response.headers);
  57. });
  58. return response;
  59. }
  60. }
  61.  
  62. module.exports = Mailer;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement