Guest User

Untitled

a guest
Aug 7th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. /*!
  2. * MailWrapper - An extremely simple nodemailer wrapper library that makes it easy to send emails using Express and Jade templates. Originally
  3. * developed to practice and learn how to write modules for node.
  4. *
  5. * Copyright(c) 2011 Tom Shaw <tom@tomshaw.info>
  6. * MIT Licensed
  7. */
  8.  
  9. /**
  10. * Module dependencies.
  11. */
  12. var nodemailer = require('nodemailer'),
  13. jade = require('jade'),
  14. path = require('path'),
  15. config = require('../config/mailer');
  16.  
  17. /**
  18. * Mailer pseudo class.
  19. *
  20. * @param obj user
  21. *
  22. */
  23. var Mailer = function Mailer(user) {
  24. if (user) this.merge(this, { _userdata: user });
  25. if (config.message) this.merge(this, config.message);
  26. if (config.server) this.merge(this, config.server);
  27. nodemailer.SMTP = this.smtp;
  28. this._path = config.templatePath;
  29. };
  30.  
  31. /**
  32. * Mailer prototype.
  33. */
  34.  
  35. Mailer.prototype = {
  36.  
  37. /**
  38. * Merges object properties.
  39. */
  40. merge: function(a, b) {
  41. if (a && b) {
  42. for (var k in b) {
  43. a[k] = b[k];
  44. }
  45. }
  46. return a;
  47. },
  48.  
  49. /**
  50. * Sets user data.
  51. *
  52. * @deprecated
  53. */
  54. set userdata(user) {
  55. this._userdata = user;
  56. },
  57.  
  58. /**
  59. * @returns User data fetched from persistence fed in thru the object constructor.
  60. */
  61. get userdata() {
  62. return this._userdata;
  63. },
  64.  
  65. /**
  66. * @returns Returns nodemailer required SMTP data.
  67. */
  68. get smtp() {
  69. return {
  70. ssl: this._ssl
  71. , host: this._host
  72. , port: this._port
  73. , domain: this._domain
  74. , use_authentication: this._use_authentication
  75. , user: this._username
  76. , pass: this._password
  77. , debug: this._debug
  78. }
  79. },
  80.  
  81. /**
  82. * Generates some basic email instructions for nodemailer.
  83. *
  84. * _to: - Who the email is being sent to.
  85. * _sender: - Who the email is being sent from.
  86. * _subject: - The defauly email subject.
  87. * _reply_to: - The email address to reply to.
  88. *
  89. * @returns Creates and returns an array of default email data.
  90. */
  91. get data() {
  92. return {
  93. to: this.userdata.email
  94. , sender: this._from
  95. , subject: this._subject
  96. , reply_to: this._reply_to
  97. }
  98. },
  99.  
  100. /**
  101. * Sends the actual email message.
  102. */
  103. send: function() {
  104. var data = this.data;
  105. var userdata = this.userdata;
  106. var debug = this._debug;
  107. console.log(this._path + this._template);
  108. jade.renderFile(this._path + this._template, { locals: { user: userdata } }, function(err, file) {
  109. if(err) console.log(err);
  110. //data.html = file;
  111. data.body = file;
  112. nodemailer.send_mail(data, function(error, success) {
  113. if(debug) {
  114. if (error) {
  115. console.log(error);
  116. } else if (success) {
  117. console.log('[SUCCESSFULL EMAIL SENT TO]: ' + userdata.email);
  118. }
  119. }
  120. });
  121. });
  122. },
  123.  
  124. /**
  125. * Returns objects properties in JSON format.
  126. */
  127. toJSON: function() {
  128. return this.data;
  129. }
  130.  
  131. };
  132.  
  133. /**
  134. * Exposed methods.
  135. */
  136.  
  137. /**
  138. * Sends a thank you message.
  139. */
  140. module.exports.sendThankyou = function(user) {
  141. var mailer = new Mailer(user);
  142. mailer._subject = 'Thank you for your purchase!';
  143. mailer._template = 'thankyou.jade';
  144. mailer.send();
  145. return mailer;
  146. }
  147.  
  148. /**
  149. * Send a registration email confirmation message.
  150. */
  151. module.exports.sendConfirmation = function(user) {
  152. var mailer = new Mailer(user);
  153. mailer._subject = 'Web Site Email Confirmation';
  154. mailer._template = 'confirmation.jade';
  155. mailer.send();
  156. return mailer;
  157. }
Add Comment
Please, Sign In to add comment