email.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //var nodemailer = require('nodemailer');
  2. // buffer each user's email text in a queue, then flush them in single email
  3. Meteor.startup(() => {
  4. Notifications.subscribe('email', (user, title, description, params) => {
  5. // add quote to make titles easier to read in email text
  6. const quoteParams = _.clone(params);
  7. ['card', 'list', 'oldList', 'board', 'comment'].forEach(key => {
  8. if (quoteParams[key]) quoteParams[key] = `"${params[key]}"`;
  9. });
  10. ['timeValue', 'timeOldValue'].forEach(key => {
  11. quoteParams[key] = quoteParams[key] ? `${params[key]}` : '';
  12. });
  13. const lan = user.getLanguage();
  14. const subject = TAPi18n.__(title, params, lan); // the original function has a fault, i believe the title should be used according to original author
  15. const existing = user.getEmailBuffer().length > 0;
  16. const htmlEnabled =
  17. Meteor.settings.public &&
  18. Meteor.settings.public.RICHER_CARD_COMMENT_EDITOR !== false;
  19. const text = `${existing ? `\n${subject}\n` : ''}${
  20. params.user
  21. } ${TAPi18n.__(description, quoteParams, lan)}\n${params.url}`;
  22. user.addEmailBuffer(htmlEnabled ? text.replace(/\n/g, '<br/>') : text);
  23. // unlike setTimeout(func, delay, args),
  24. // Meteor.setTimeout(func, delay) does not accept args :-(
  25. // so we pass userId with closure
  26. const userId = user._id;
  27. Meteor.setTimeout(() => {
  28. const user = Users.findOne(userId);
  29. // for each user, in the timed period, only the first call will get the cached content,
  30. // other calls will get nothing
  31. const texts = user.getEmailBuffer();
  32. if (texts.length === 0) return;
  33. // merge the cached content into single email and flush
  34. const html = texts.join('<br/>\n\n');
  35. user.clearEmailBuffer();
  36. try {
  37. /*
  38. if (process.env.MAIL_SERVICE !== '') {
  39. let transporter = nodemailer.createTransport({
  40. service: process.env.MAIL_SERVICE,
  41. auth: {
  42. user: process.env.MAIL_SERVICE_USER,
  43. pass: process.env.MAIL_SERVICE_PASSWORD
  44. },
  45. })
  46. let info = transporter.sendMail({
  47. to: user.emails[0].address.toLowerCase(),
  48. from: Accounts.emailTemplates.from,
  49. subject,
  50. html,
  51. })
  52. } else {
  53. Email.send({
  54. to: user.emails[0].address.toLowerCase(),
  55. from: Accounts.emailTemplates.from,
  56. subject,
  57. html,
  58. });
  59. }
  60. */
  61. Email.send({
  62. to: user.emails[0].address.toLowerCase(),
  63. from: Accounts.emailTemplates.from,
  64. subject,
  65. html,
  66. });
  67. } catch (e) {
  68. return;
  69. }
  70. }, process.env.EMAIL_NOTIFICATION_TIMEOUT || 30000);
  71. });
  72. });