Guest User

Untitled

a guest
Jan 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. class SendgridService {
  2. constructor(config) {
  3. this.client = sendgrid(config.sendgrid.api_key);
  4. }
  5. send(options) {
  6. return new Promise((resolve, reject) => {
  7. this.client.send(options, function(error, result) {
  8. if (error) {
  9. console.error("(send) Failure - Message: %j Error: %j", options, error);
  10. return reject(error);
  11. }
  12.  
  13. console.log("(send) Success - Message: %j Result: %j", options, result);
  14. return resolve(result);
  15. });
  16. });
  17. }
  18. }
  19.  
  20. class EmailService {
  21. constructor(options, config = Config) {
  22. this.config = config;
  23. this.provider = new Sendgrid(options, config);
  24. }
  25. send(options, record_info) {
  26. if (_.contains([ "sandbox", "dev" ], this.config.env.name)) {
  27. options.to = this.config.email.test_recipient;
  28. options.bcc = undefined;
  29. // other test related stuff
  30. }
  31.  
  32. console.log("Sending email from: %s to: %s", options.from, options.to);
  33. return this.provider.send(options)
  34. .then(result => {
  35. // some log/operation in case of success
  36. return result;
  37. })
  38. .catch(error => {
  39. // some log/operation in case of error, can be fallback or retry
  40. return Promise.reject(error);
  41. });
  42. }
  43. };
Add Comment
Please, Sign In to add comment