email.js 2.8 KB

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