-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemailer.js
76 lines (58 loc) · 1.26 KB
/
emailer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* dependencies
*/
var nodemailer = require('nodemailer'),
events = require('events'),
util = require('util');
/**
* @constructor
* @description Generates a Emailer object
*/
function Emailer(){
// create emitter
events.EventEmitter.call(this);
};
/**
* extend the prototype & export the module
*/
util.inherits(Emailer, events.EventEmitter);
exports.Emailer = Emailer;
/**
* @method send
* @memberOf Emailer
* @param {Object} message
* @param {Object} transport
* @description Sends a message
*/
Emailer.prototype.send = function(message, transport){
var self = this;
// get the mail transport
var trans = this.getTransport(transport);
// send the email
trans.sendMail(message, function(error, response){
if (error) {
self.emit('mailer:error', error);
} else {
self.emit('mailer:success', response);
}
});
};
/**
* @method getTransport
* @memberOf Emailer
* @param {Object} transport
* @description Generates a transport
*/
Emailer.prototype.getTransport = function(transport){
// get options
var options = transport;
return nodemailer.createTransport(options.type, {
host: options.host,
secureConnection: options.secureConnection,
port: options.port,
auth: {
user: options.user,
pass: options.pass
}
});
};