mailer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * mailer
  3. */
  4. module.exports = function(crowi) {
  5. 'use strict';
  6. var debug = require('debug')('growi:lib:mailer')
  7. , nodemailer = require('nodemailer')
  8. , swig = require('swig-templates')
  9. , Config = crowi.model('Config')
  10. , config = crowi.getConfig()
  11. , mailConfig = {}
  12. , mailer = {}
  13. , MAIL_TEMPLATE_DIR = crowi.mailDir
  14. ;
  15. function createSMTPClient(option)
  16. {
  17. var client;
  18. debug('createSMTPClient option', option);
  19. if (!option) {
  20. option = {
  21. host: config.crowi['mail:smtpHost'],
  22. port: config.crowi['mail:smtpPort'],
  23. };
  24. if (config.crowi['mail:smtpUser'] && config.crowi['mail:smtpPassword']) {
  25. option.auth = {
  26. user: config.crowi['mail:smtpUser'],
  27. pass: config.crowi['mail:smtpPassword']
  28. };
  29. }
  30. if (option.port === 465) {
  31. option.secure = true;
  32. }
  33. }
  34. option.tls = {rejectUnauthorized: false};
  35. client = nodemailer.createTransport(option);
  36. debug('mailer set up for SMTP', client);
  37. return client;
  38. }
  39. function createSESClient(option)
  40. {
  41. var client;
  42. if (!option) {
  43. option = {
  44. accessKeyId: config.crowi['aws:accessKeyId'],
  45. secretAccessKey: config.crowi['aws:secretAccessKey']
  46. };
  47. }
  48. var ses = require('nodemailer-ses-transport');
  49. client = nodemailer.createTransport(ses(option));
  50. debug('mailer set up for SES', client);
  51. return client;
  52. }
  53. function initialize() {
  54. if (!config.crowi['mail:from']) {
  55. mailer = undefined;
  56. return;
  57. }
  58. if (config.crowi['mail:smtpHost'] && config.crowi['mail:smtpPort']
  59. ) {
  60. // SMTP 設定がある場合はそれを優先
  61. mailer = createSMTPClient();
  62. } else if (config.crowi['aws:accessKeyId'] && config.crowi['aws:secretAccessKey']) {
  63. // AWS 設定がある場合はSESを設定
  64. mailer = createSESClient();
  65. } else {
  66. mailer = undefined;
  67. }
  68. mailConfig.from = config.crowi['mail:from'];
  69. mailConfig.subject = Config.appTitle(config) + 'からのメール';
  70. debug('mailer initialized');
  71. }
  72. function setupMailConfig (overrideConfig) {
  73. var c = overrideConfig
  74. , mc = {}
  75. ;
  76. mc = mailConfig;
  77. mc.to = c.to;
  78. mc.from = c.from || mailConfig.from;
  79. mc.text = c.text;
  80. mc.subject = c.subject || mailConfig.subject;
  81. return mc;
  82. }
  83. function send(config, callback) {
  84. if (mailer) {
  85. var templateVars = config.vars || {};
  86. return swig.renderFile(
  87. MAIL_TEMPLATE_DIR + config.template,
  88. templateVars,
  89. function (err, output) {
  90. if (err) {
  91. throw err;
  92. }
  93. config.text = output;
  94. return mailer.sendMail(setupMailConfig(config), callback);
  95. }
  96. );
  97. } else {
  98. debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
  99. return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null);
  100. }
  101. }
  102. initialize();
  103. return {
  104. createSMTPClient: createSMTPClient,
  105. createSESClient: createSESClient,
  106. mailer: mailer,
  107. send: send,
  108. };
  109. };