| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | var	fs = require('fs'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2014-02-14 11:58:51 -05:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2014-06-25 18:55:22 -04:00
										 |  |  | 	templates = require('templates.js'), | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User = require('./user'), | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 	Plugins = require('./plugins'), | 
					
						
							| 
									
										
										
										
											2013-12-31 09:33:25 -05:00
										 |  |  | 	Meta = require('./meta'), | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 	translator = require('../public/src/translator'), | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | 	app = {}, | 
					
						
							| 
									
										
										
										
											2014-01-04 18:03:54 -05:00
										 |  |  | 	Emailer = {}; | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | Emailer.registerApp = function(expressApp) { | 
					
						
							|  |  |  | 	app = expressApp; | 
					
						
							|  |  |  | 	return Emailer; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Emailer.send = function(template, uid, params) { | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		html: function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | 			app.render('emails/' + template, params, next); | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		plaintext: function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-13 00:49:32 -04:00
										 |  |  | 			app.render('emails/' + template + '_plaintext', params, next); | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		email: async.apply(User.getUserField, uid, 'email'), | 
					
						
							|  |  |  | 		settings: async.apply(User.getSettings, uid) | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 	}, function(err, results) { | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 		async.map([results.html, results.plaintext, params.subject], function(raw, next) { | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 			translator.translate(raw, results.settings.language || meta.config.defaultLang || 'en_GB', function(translated) { | 
					
						
							|  |  |  | 				next(undefined, translated); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, function(err, translated) { | 
					
						
							| 
									
										
										
										
											2014-02-05 18:25:13 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return winston.error(err.message); | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 			} else if (!results.email) { | 
					
						
							| 
									
										
										
										
											2014-02-05 18:25:13 -05:00
										 |  |  | 				return winston.warn('uid : ' + uid + ' has no email, not sending.'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Plugins.fireHook('action:email.send', { | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 				to: results.email, | 
					
						
							| 
									
										
										
										
											2014-02-05 18:25:13 -05:00
										 |  |  | 				from: Meta.config['email:from'] || 'no-reply@localhost.lan', | 
					
						
							| 
									
										
										
										
											2014-09-16 13:27:26 -04:00
										 |  |  | 				subject: translated[2], | 
					
						
							| 
									
										
										
										
											2014-06-27 14:52:12 -04:00
										 |  |  | 				html: translated[0], | 
					
						
							|  |  |  | 				plaintext: translated[1], | 
					
						
							| 
									
										
										
										
											2014-02-05 18:25:13 -05:00
										 |  |  | 				template: template, | 
					
						
							|  |  |  | 				uid: uid | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-12-30 14:02:43 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = Emailer; |