-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailNotifications.js
More file actions
57 lines (46 loc) · 1.22 KB
/
EmailNotifications.js
File metadata and controls
57 lines (46 loc) · 1.22 KB
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
var fs = require('fs');
var nodemailer = require('nodemailer');
var htmlToText = require('nodemailer-html-to-text').htmlToText;
module.exports = function(config){
var exports = {};
var emailTemplate = "";
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: config.email.user,
pass: config.email.password
}
});
transporter.use('compile', htmlToText());
[
'emailNotificationTemplate',
'emailNotificationAttachments',
'emailNotificationSubject'
].forEach(function(option){
if( !config.hasOwnProperty(option) ){
throw new Error("Missing config option: {0}".format(option));
}
});
fs.readFile(config.emailNotificationTemplate,'utf8',function(err,contents){
if( err ) throw err;
emailTemplate = contents.toString();
});
exports.sendEmail = function(address,data){
var options = {
from: config.email.from,
to: address,
subject: config.emailNotificationSubject.format(data),
html: emailTemplate.format(data),
attachments: config.emailNotificationAttachments
}
transporter.sendMail(options,function(error,info){
if(error){
console.error(error);
}
if( info ){
console.log(info.response);
}
});
}
return exports;
}