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. ;
  14. function createSMTPClient(option) {
  15. var client;
  16. debug('createSMTPClient option', option);
  17. if (!option) {
  18. option = {
  19. host: config.crowi['mail:smtpHost'],
  20. port: config.crowi['mail:smtpPort'],
  21. };
  22. if (config.crowi['mail:smtpUser'] && config.crowi['mail:smtpPassword']) {
  23. option.auth = {
  24. user: config.crowi['mail:smtpUser'],
  25. pass: config.crowi['mail:smtpPassword']
  26. };
  27. }
  28. if (option.port === 465) {
  29. option.secure = true;
  30. }
  31. }
  32. option.tls = {rejectUnauthorized: false};
  33. client = nodemailer.createTransport(option);
  34. debug('mailer set up for SMTP', client);
  35. return client;
  36. }
  37. function createSESClient(option) {
  38. var client;
  39. if (!option) {
  40. option = {
  41. accessKeyId: config.crowi['aws:accessKeyId'],
  42. secretAccessKey: config.crowi['aws:secretAccessKey']
  43. };
  44. }
  45. var ses = require('nodemailer-ses-transport');
  46. client = nodemailer.createTransport(ses(option));
  47. debug('mailer set up for SES', client);
  48. return client;
  49. }
  50. function initialize() {
  51. if (!config.crowi['mail:from']) {
  52. mailer = undefined;
  53. return;
  54. }
  55. if (config.crowi['mail:smtpHost'] && config.crowi['mail:smtpPort']
  56. ) {
  57. // SMTP 設定がある場合はそれを優先
  58. mailer = createSMTPClient();
  59. }
  60. else if (config.crowi['aws:accessKeyId'] && config.crowi['aws:secretAccessKey']) {
  61. // AWS 設定がある場合はSESを設定
  62. mailer = createSESClient();
  63. }
  64. else {
  65. mailer = undefined;
  66. }
  67. mailConfig.from = config.crowi['mail:from'];
  68. mailConfig.subject = Config.appTitle(config) + 'からのメール';
  69. debug('mailer initialized');
  70. }
  71. function setupMailConfig(overrideConfig) {
  72. var c = overrideConfig
  73. , mc = {}
  74. ;
  75. mc = mailConfig;
  76. mc.to = c.to;
  77. mc.from = c.from || mailConfig.from;
  78. mc.text = c.text;
  79. mc.subject = c.subject || mailConfig.subject;
  80. return mc;
  81. }
  82. function send(config, callback) {
  83. if (mailer) {
  84. var templateVars = config.vars || {};
  85. return swig.renderFile(
  86. config.template,
  87. templateVars,
  88. function(err, output) {
  89. if (err) {
  90. throw err;
  91. }
  92. config.text = output;
  93. return mailer.sendMail(setupMailConfig(config), callback);
  94. }
  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. };