diff --git a/src/mail.js b/src/mail.js index ec542267a..2a720bbd4 100644 --- a/src/mail.js +++ b/src/mail.js @@ -12,6 +12,8 @@ exports = module.exports = { startMail: restartMail, + sendTestMail: sendTestMail, + MailError: MailError }; @@ -26,6 +28,7 @@ var assert = require('assert'), domains = require('./domains.js'), infra = require('./infra_version.js'), maildb = require('./maildb.js'), + mailer = require('./mailer.js'), net = require('net'), nodemailer = require('nodemailer'), os = require('os'), @@ -604,3 +607,16 @@ function setMailEnabled(domain, enabled, callback) { callback(null); }); } + +function sendTestMail(domain, to, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof to, 'object'); + + get(domain, function (error, result) { + if (error) return callback(error); + + mailer.sendTestMail(result.domain, to); + + callback(); + }); +} \ No newline at end of file diff --git a/src/mailer.js b/src/mailer.js index b22f1a4e1..23d002af7 100644 --- a/src/mailer.js +++ b/src/mailer.js @@ -530,14 +530,15 @@ function unexpectedExit(program, context, callback) { }); } -function sendTestMail(email) { +function sendTestMail(domain, email) { + assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof email, 'string'); getMailConfig(function (error, mailConfig) { if (error) return debug('Error getting mail details:', error); var mailOptions = { - from: mailConfig.notificationFrom, + from: `no-reply@${domain}`, to: email, subject: util.format('Test Email from %s', mailConfig.cloudronName), text: render('test.ejs', { cloudronName: mailConfig.cloudronName, format: 'text'}) diff --git a/src/routes/cloudron.js b/src/routes/cloudron.js index 56127cc42..4cef505a6 100644 --- a/src/routes/cloudron.js +++ b/src/routes/cloudron.js @@ -15,8 +15,7 @@ exports = module.exports = { feedback: feedback, checkForUpdates: checkForUpdates, getLogs: getLogs, - getLogStream: getLogStream, - sendTestMail: sendTestMail + getLogStream: getLogStream }; var appstore = require('../appstore.js'), @@ -32,7 +31,6 @@ var appstore = require('../appstore.js'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess, progress = require('../progress.js'), - mailer = require('../mailer.js'), superagent = require('superagent'), updateChecker = require('../updatechecker.js'), _ = require('underscore'); @@ -302,13 +300,3 @@ function getLogStream(req, res, next) { logStream.on('error', res.end.bind(res, null)); }); } - -function sendTestMail(req, res, next) { - assert.strictEqual(typeof req.body, 'object'); - - if (!req.body.email || typeof req.body.email !== 'string') return next(new HttpError(400, 'email must be a non-empty string')); - - mailer.sendTestMail(req.body.email); - - next(new HttpSuccess(202)); -} diff --git a/src/routes/mail.js b/src/routes/mail.js index e17cfb3a1..37a2fe97b 100644 --- a/src/routes/mail.js +++ b/src/routes/mail.js @@ -8,7 +8,9 @@ exports = module.exports = { setMailFromValidation: setMailFromValidation, setCatchAllAddress: setCatchAllAddress, setMailRelay: setMailRelay, - setMailEnabled: setMailEnabled + setMailEnabled: setMailEnabled, + + sendTestMail: sendTestMail }; var assert = require('assert'), @@ -106,3 +108,17 @@ function setMailEnabled(req, res, next) { next(new HttpSuccess(202)); }); } + +function sendTestMail(req, res, next) { + assert.strictEqual(typeof req.params.domain, 'string'); + assert.strictEqual(typeof req.body, 'object'); + + if (!req.body.to || typeof req.body.to !== 'string') return next(new HttpError(400, 'to must be a non-empty string')); + + mail.sendTestMail(req.params.domain, req.body.to, function (error) { + if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message)); + if (error) return next(new HttpError(500, error)); + + next(new HttpSuccess(202)); + }); +} diff --git a/src/server.js b/src/server.js index c681ca3c2..655ee2ff3 100644 --- a/src/server.js +++ b/src/server.js @@ -122,7 +122,6 @@ function initializeExpressSync() { router.get ('/api/v1/cloudron/ssh/authorized_keys/:identifier', cloudronScope, routes.user.requireAdmin, routes.ssh.getAuthorizedKey); router.del ('/api/v1/cloudron/ssh/authorized_keys/:identifier', cloudronScope, routes.user.requireAdmin, routes.ssh.delAuthorizedKey); router.get ('/api/v1/cloudron/eventlog', cloudronScope, routes.user.requireAdmin, routes.eventlog.get); - router.post('/api/v1/cloudron/send_test_mail', cloudronScope, routes.user.requireAdmin, routes.cloudron.sendTestMail); // profile api, working off the user behind the provided token router.get ('/api/v1/profile', profileScope, routes.profile.get); @@ -219,6 +218,7 @@ function initializeExpressSync() { router.post('/api/v1/mail/:domain/catch_all', settingsScope, routes.user.requireAdmin, routes.mail.setCatchAllAddress); router.post('/api/v1/mail/:domain/relay', settingsScope, routes.user.requireAdmin, routes.mail.setMailRelay); router.post('/api/v1/mail/:domain/enable', settingsScope, routes.user.requireAdmin, routes.mail.setMailEnabled); + router.post('/api/v1/mail/:domain/send_test_mail', cloudronScope, routes.user.requireAdmin, routes.mail.sendTestMail); // feedback router.post('/api/v1/feedback', usersScope, routes.cloudron.feedback);