فهرست منبع

Refactor mailer: user swig and tempalte file to build mail body

Sotaro KARASAWA 11 سال پیش
والد
کامیت
fdc61eb36a
4فایلهای تغییر یافته به همراه59 افزوده شده و 11 حذف شده
  1. 16 2
      lib/mailer.js
  2. 8 9
      models/user.js
  3. 14 0
      views/mail/admin/userInvitation.txt
  4. 21 0
      views/mail/admin/userWaitingActivation.txt

+ 16 - 2
lib/mailer.js

@@ -6,10 +6,12 @@ module.exports = function(app) {
   'use strict';
   'use strict';
 
 
   var debug = require('debug')('crowi:lib:mailer')
   var debug = require('debug')('crowi:lib:mailer')
-    , nodemailer = require("nodemailer")
+    , nodemailer = require('nodemailer')
+    , swig = require('swig')
     , config = app.set('config')
     , config = app.set('config')
     , mailConfig = {}
     , mailConfig = {}
     , mailer = {}
     , mailer = {}
+    , MAIL_TEMPLATE_DIR = app.set('views') + '/mail/'
     ;
     ;
 
 
 
 
@@ -100,7 +102,19 @@ module.exports = function(app) {
 
 
   function send(config, callback) {
   function send(config, callback) {
     if (mailer) {
     if (mailer) {
-      return mailer.sendMail(setupMailConfig(config), callback);
+      var templateVars = config.vars || {};
+      return swig.renderFile(
+        MAIL_TEMPLATE_DIR + config.template,
+        templateVars,
+        function (err, output) {
+          if (err) {
+            throw err;
+          }
+
+          config.text = output;
+          return mailer.sendMail(setupMailConfig(config), callback);
+        }
+      );
     } else {
     } else {
       debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
       debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
       return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null);
       return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null);

+ 8 - 9
models/user.js

@@ -405,20 +405,19 @@ module.exports = function(app, models) {
             createdUserList,
             createdUserList,
             function(user, next) {
             function(user, next) {
               if (user.password === null) {
               if (user.password === null) {
-                next();
+                return next();
               }
               }
 
 
               mailer.send({
               mailer.send({
                   to: user.email,
                   to: user.email,
                   subject: 'Invitation to ' + config.crowi['app:title'],
                   subject: 'Invitation to ' + config.crowi['app:title'],
-                  text: 'Hi, ' + user.email + '\n\n'
-                    + 'You are invited to our Wiki, you can log in with following account:\n\n'
-                    + 'Email: ' + user.email + '\n'
-                    + 'Password: ' + user.password
-                    + '\n (This password was auto generated. Update required at the first time you logging in)\n'
-                    + '\n'
-                    + 'We are waiting for you!\n'
-                    + config.crowi['app:url']
+                  template: 'admin/userInvitation.txt',
+                  vars: {
+                    email: user.email,
+                    password: user.password,
+                    url: config.crowi['app:url'],
+                    appTitle: config.crowi['app:title'],
+                  }
                 },
                 },
                 function (err, s) {
                 function (err, s) {
                   debug('completed to send email: ', err, s);
                   debug('completed to send email: ', err, s);

+ 14 - 0
views/mail/admin/userInvitation.txt

@@ -0,0 +1,14 @@
+Hi, {{ email }}
+
+You are invited to our Wiki, you can log in with following account:
+
+Email: {{ email }}
+Password: {{ password }}
+(This password was auto generated. Update required at the first time you logging in)
+
+We are waiting for you!
+{{ url }}
+
+--
+{{ appTitle }}
+{{ url }}

+ 21 - 0
views/mail/admin/userWaitingActivation.txt

@@ -0,0 +1,21 @@
+Hi, {{ adminUser.name }}
+
+A user registered to {{ appTitle }}.
+
+
+====
+Created user:
+
+Name: {{ createdUser.name }}
+User Name: {{ createdUser.username }}
+Email: {{ createdUser.email }}
+====
+
+Please do some action with following URL:
+{{ url }}/admin/user
+
+
+--
+{{ appTitle }}
+{{ url }}
+