email.js 2.8 KB

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