mail.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. const logger = require('@alias/logger')('growi:service:mail');
  2. const nodemailer = require('nodemailer');
  3. const swig = require('swig-templates');
  4. const S2sMessage = require('../models/vo/s2s-message');
  5. const S2sMessageHandlable = require('./s2s-messaging/handlable');
  6. class MailService extends S2sMessageHandlable {
  7. constructor(crowi) {
  8. super();
  9. this.appService = crowi.appService;
  10. this.configManager = crowi.configManager;
  11. this.s2sMessagingService = crowi.s2sMessagingService;
  12. this.mailConfig = {};
  13. this.mailer = {};
  14. this.initialize();
  15. }
  16. /**
  17. * @inheritdoc
  18. */
  19. shouldHandleS2sMessage(s2sMessage) {
  20. const { eventName, updatedAt } = s2sMessage;
  21. if (eventName !== 'mailServiceUpdated' || updatedAt == null) {
  22. return false;
  23. }
  24. return this.lastLoadedAt == null || this.lastLoadedAt < new Date(s2sMessage.updatedAt);
  25. }
  26. /**
  27. * @inheritdoc
  28. */
  29. async handleS2sMessage(s2sMessage) {
  30. const { configManager } = this;
  31. logger.info('Initialize mail settings by pubsub notification');
  32. await configManager.loadConfigs();
  33. this.initialize();
  34. }
  35. async publishUpdatedMessage() {
  36. const { s2sMessagingService } = this;
  37. if (s2sMessagingService != null) {
  38. const s2sMessage = new S2sMessage('mailServiceUpdated', { updatedAt: new Date() });
  39. try {
  40. await s2sMessagingService.publish(s2sMessage);
  41. }
  42. catch (e) {
  43. logger.error('Failed to publish update message with S2sMessagingService: ', e.message);
  44. }
  45. }
  46. }
  47. initialize() {
  48. const { appService, configManager } = this;
  49. if (!configManager.getConfig('crowi', 'mail:from')) {
  50. this.mailer = null;
  51. return;
  52. }
  53. const transmissionMethod = configManager.getConfig('crowi', 'mail:transmissionMethod');
  54. if (transmissionMethod === 'smtp') {
  55. this.mailer = this.createSMTPClient();
  56. }
  57. else if (transmissionMethod === 'ses') {
  58. this.mailer = this.createSESClient();
  59. }
  60. else {
  61. this.mailer = null;
  62. }
  63. this.mailConfig.from = configManager.getConfig('crowi', 'mail:from');
  64. this.mailConfig.subject = `${appService.getAppTitle()}からのメール`;
  65. logger.debug('mailer initialized');
  66. }
  67. createSMTPClient(option) {
  68. const { configManager } = this;
  69. logger.debug('createSMTPClient option', option);
  70. if (!option) {
  71. option = { // eslint-disable-line no-param-reassign
  72. host: configManager.getConfig('crowi', 'mail:smtpHost'),
  73. port: configManager.getConfig('crowi', 'mail:smtpPort'),
  74. };
  75. if (configManager.getConfig('crowi', 'mail:smtpUser') && configManager.getConfig('crowi', 'mail:smtpPassword')) {
  76. option.auth = {
  77. user: configManager.getConfig('crowi', 'mail:smtpUser'),
  78. pass: configManager.getConfig('crowi', 'mail:smtpPassword'),
  79. };
  80. }
  81. if (option.port === 465) {
  82. option.secure = true;
  83. }
  84. }
  85. option.tls = { rejectUnauthorized: false };
  86. const client = nodemailer.createTransport(option);
  87. logger.debug('mailer set up for SMTP', client);
  88. return client;
  89. }
  90. createSESClient(option) {
  91. const { configManager } = this;
  92. if (!option) {
  93. option = { // eslint-disable-line no-param-reassign
  94. accessKeyId: configManager.getConfig('crowi', 'mail:sesAccessKeyId'),
  95. secretAccessKey: configManager.getConfig('crowi', 'mail:sesSecretAccessKey'),
  96. };
  97. }
  98. const ses = require('nodemailer-ses-transport');
  99. const client = nodemailer.createTransport(ses(option));
  100. logger.debug('mailer set up for SES', client);
  101. return client;
  102. }
  103. setupMailConfig(overrideConfig) {
  104. const c = overrideConfig;
  105. let mc = {};
  106. mc = this.mailConfig;
  107. mc.to = c.to;
  108. mc.from = c.from || this.mailConfig.from;
  109. mc.text = c.text;
  110. mc.subject = c.subject || this.mailConfig.subject;
  111. return mc;
  112. }
  113. async send(config) {
  114. if (this.mailer == null) {
  115. throw new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
  116. }
  117. const templateVars = config.vars || {};
  118. const output = await swig.renderFile(
  119. config.template,
  120. templateVars,
  121. );
  122. config.text = output;
  123. return this.mailer.sendMail(this.setupMailConfig(config));
  124. }
  125. }
  126. module.exports = MailService;