Permalink
Browse files

First mail util improvement

  • Loading branch information...
1 parent 7afd0d8 commit 9dad2d94b6d83c60225c653e6451168f45798641 @anddimario committed Jul 23, 2012
Showing with 79 additions and 40 deletions.
  1. +33 −40 controller/base.js
  2. +2 −0 controller/user.js
  3. +1 −0 lib/newsite.js
  4. +41 −0 lib/utils.js
  5. +1 −0 package.json
  6. +1 −0 views/default.mustache
View
@@ -747,57 +747,50 @@ function route() {
var data = {
captcha_public_key: results[0].captcha_public_key,
captcha_private_key: results[0].captcha_private_key,
- search_collection: results[0].search_collection
+ search_collection: results[0].search_collection,
+ smtp_settings: results[0].smtp_settings
};
utils.rendering(req.headers.host, 'information', data, req.session.info, req.session.lang, message, function callback(layout) {
res.end(layout);
});
});
});
router.post('/information', utils.restricted_module, utils.restricted, function (req, res) {
- var validator = new Validator();
- validator.check(req.body.captcha_public_key, 'recaptcha public key').notEmpty();
- validator.check(req.body.captcha_private_key, 'recaptch private key').notEmpty();
- validator.check(req.body.search_collection, 'search collection').notEmpty();
- var errors = validator.getErrors();
- if (errors.length)
- {
- var message = {
- action: 'alert-error',
- reference: 'required',
- value: errors
- };
- var data = {
- captcha_public_key: req.body.captcha_public_key,
- captcha_private_key: req.body.captcha_private_key,
- search_collection: req.body.search_collection
+ var find = {
+ type: 'information'
+ };
+ var value = {
+ captcha_public_key: req.body.captcha_public_key,
+ captcha_private_key: req.body.captcha_private_key,
+ search_collection: req.body.search_collection
+ };
+ // split and put smtp settings into collection
+ if (req.body.smtp_settings) {
+ var temp = req.body.smtp_settings;
+ temp = temp.split(',');
+ value.smtp_host = temp[0];
+ value.smtp_user = temp[1];
+ value.smtp_password = temp[2];
+ if (temp[3] === 'false') {
+ value.smtp_ssl = false;
+ } else {
+ value.smtp_ssl = true;
}
- utils.rendering(req.headers.host, 'content', data, req.session.info, req.session.lang, message, function callback(layout) {
+ value.smtp_from = temp[4];
+ }
+ var information_update = new ModelsBase(req.headers.host);
+ information_update.update(find, value, function callback(results) {
+ var data = {}
+ var message = {
+ action: '',
+ reference: 'waitrefresh',
+ value: ''
+ };
+ utils.rendering(req.headers.host, 'information', data, req.session.info, req.session.lang, message, function callback(layout) {
res.end(layout);
});
- } else {
- var find = {
- type: 'information'
- };
- var value = {
- captcha_public_key: req.body.captcha_public_key,
- captcha_private_key: req.body.captcha_private_key,
- search_collection: req.body.search_collection
- };
- var information_update = new ModelsBase(req.headers.host);
- information_update.update(find, value, function callback(results) {
- var data = {}
- var message = {
- action: '',
- reference: 'waitrefresh',
- value: ''
- };
- utils.rendering(req.headers.host, 'information', data, req.session.info, req.session.lang, message, function callback(layout) {
- res.end(layout);
- });
- });
+ });
- }
});
// not language or similar for routing, to limit conflict with language route above
View
@@ -220,6 +220,8 @@ function route() {
value.role = "user";
}
user_insert.insert(value, function callbacks(results) {
+ // send email to user, use function in lib/utils
+ utils.send_email(req.headers.host, req.body.email, 'registration', req.body);
var message = {
action: '',
reference: 'registrated',
View
@@ -91,6 +91,7 @@ var defaultcontent = [
{ "type": "content", "tag": "langformcontenttype", "lang_id": "0", "text": "Content Type" },
{ "type": "content", "tag": "langformcontent", "lang_id": "0", "text": "Content" },
{ "type": "content", "tag": "langformkeyword", "lang_id": "0", "text": "Keywords for searching, like (key1,key2,...)" },
+{ "type": "content", "tag": "langformsmtpsettings", "lang_id": "0", "text": "SMTP Settings, like: host,username,password,ssl(true or false),from(Acme Ltd <info@acme.com>" },
]
var database, information = {}, domain, subdomains, count = 0;
View
@@ -6,6 +6,7 @@ var fs = require('fs');
var cache = require('memory-cache')
var Hogan = require('hogan.js');
var Config = require('../config');
+var email = require('emailjs');
function quicklog(s) {
// SYNC TO REMOVE REPLACE WHEN WRITE AT THE SAME TIME
@@ -217,9 +218,49 @@ function maintenance(req, res, next) {
});
}
+/*
+domain: the site that send the email
+receiver: the mail receiver
+mail_type: the mail type
+mail_information: information useful for template
+*/
+function send_email(domain, mail_receiver, mail_type, mail_information) {
+ // get information and template from cache
+ var site = cache.get(domain);
+ site.forEach(function (row) {
+ if (row.type === "information") {
+ var server = email.server.connect({
+ user: row.smtp_user,
+ password: row.smtp_password,
+ host: row.smtp_host,
+ ssl: row.smtp_ssl
+
+ });
+console.log(email,server);
+// TODO SEE how it's better to improve: use template in collection information
+// type: 'template', tag: mail_type, text: '..', subject : '..'
+ var mail_text = "Benvenuto"+mail_information;
+ var mail_subject = "Registrazione";
+
+ // send the message and get a callback with an error or details of the message that was sent
+ server.send({
+ text: mail_text,
+ from: row.smtp_from,
+ to: mail_receiver,
+ subject: mail_subject
+ }, function(err, message) {
+ if (err) {
+ quicklog("Email log: ", err);
+ }
+ });
+ }
+ });
+}
+
exports.quicklog = quicklog;
exports.auth_yet = auth_yet;
exports.rendering = rendering;
exports.restricted = restricted;
exports.restricted_module = restricted_module;
exports.maintenance = maintenance;
+exports.send_email = send_email;
View
@@ -9,6 +9,7 @@
"dependencies":{
"memory-cache":"*",
"route66":"*",
+ "emailjs":"*",
"mongodb":"*",
"validator":"*",
"hogan.js":"*",
@@ -219,6 +219,7 @@
<input type='text' name='captcha_public_key' placeholder='{{langformrecaptchapublic}}' value='{{captcha_public_key}}'/>
<input type='text' name='captcha_private_key' placeholder='{{langformrecaptchaprivate}}' value='{{captcha_private_key}}'/>
<input type='text' name='search_collection' placeholder='{{langformsearchcollection}}' value='{{search_collection}}'/>
+ <input type='text' name='smtp_settings' placeholder='{{langformsmtpsettings}}' value='{{smtp_settings}}'/>
<button type="submit" class="btn btn-primary">Invia</button>
</form>
{{/form_information}}

0 comments on commit 9dad2d9

Please sign in to comment.