forked from diogomartino/SAMPMailJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsampmail.js
153 lines (128 loc) · 5.05 KB
/
sampmail.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const http = require('http');
const url = require('url');
const nodemailer = require('nodemailer');
const fs = require('fs');
var templateFolder = __dirname + '/templates/';
var config;
try {
config = JSON.parse(fs.readFileSync(__dirname + '/config.json', 'utf8'));
console.log('Loaded configs from config.json');
if(config.enableDebug) {
console.log('\n\n');
console.log(config);
console.log('\n\n');
}
} catch (e) {
console.log('Couldn\'t read from config.json');
process.exit();
}
if (!fs.existsSync(templateFolder)) {
console.log('Warning: templates folder doesn\'t exist, the script will crash if you try to use a template. Please create the directory ' + templateFolder + '\n');
}
var transporter = nodemailer.createTransport(config.smtp);
createServer();
function logResponse(address, code, text, res) {
res.writeHead(code, {
'Content-Type': 'text/html;charset=UTF-8'
});
res.write('Powered by SAMPMailJS</br>' + code + ': ' + text);
res.end();
var consoleText = text.replace('</br>', '\n');
console.log('Sent HTTP code ' + code + ' to ' + address + ': ' + consoleText);
}
function createServer() {
var server = http.createServer(function(req, res) {
var urlObj = url.parse(req.url, true);
if (urlObj.pathe_Name == '/favicon.ico') {
res.end();
return;
}
console.log('Client (' + req.connection.remoteAddress + ') requested ' + req.url);
if (urlObj['query']['pw'] != config.httpPassword || typeof urlObj['query']['pw'] === 'undefined' || urlObj['query']['pw'] == null || urlObj['query']['pw'].replace(/\s/g, '').length < 1) {
logResponse(req.connection.remoteAddress, 403, 'Wrong password', res);
return;
}
if (urlObj['query']['action'] == 'sendm' || urlObj['query']['action'] == 'sendmtmp') {
if (req.method == 'POST') {
var body = '';
req.on('data', function(data) {
body += data;
});
req.on('end', function() {
var postArr = body.split('|');
var e_Name = postArr[0];
var e_To = postArr[1];
var e_Subject = postArr[2];
var e_Text = postArr[3];
if (urlObj['query']['action'] == 'sendmtmp') {
var templateName = postArr[4];
fs.readFile(templateFolder + templateName, 'utf8', function(err, data) {
if (err) {
logResponse(req.connection.remoteAddress, 400, 'An error ocurred:</br>' + err, res);
return;
}
var textArr = e_Text.split('#');
var templateText = data.toString();
for (var i = 0; i < textArr.length; i++) {
var tagArr = textArr[i].split(':');
templateText = templateText.replace(tagArr[0], tagArr[1]);
}
if (config.enableDebug) {
console.log('================ SENDING EMAIL ================');
console.log('From: ' + e_Name);
console.log('To: ' + e_To);
console.log('Subject: ' + e_Subject);
console.log('Text: ' + templateText);
console.log('===============================================');
}
var mailOptions = {
from: e_Name + ' <' + config.smtp.auth.user + '>',
to: e_To,
subject: e_Subject,
html: templateText
}
transporter.sendMail(mailOptions, function(err, asd) {
if (err) {
logResponse(req.connection.remoteAddress, 400, 'An error ocurred:</br>' + err, res);
return;
} else {
logResponse(req.connection.remoteAddress, 200, 'Email sent successfully', res);
return;
}
})
});
} else {
if (config.enableDebug) {
console.log('================ SENDING EMAIL ================')
console.log('De: ' + e_Name);
console.log('To: ' + e_To);
console.log('Subject: ' + e_Subject);
console.log('Text: ' + e_Text);
console.log('===============================================')
}
var mailOptions = {
from: e_Name + ' <' + config.smtp.auth.user + '>',
to: e_To,
subject: e_Subject,
html: e_Text
}
transporter.sendMail(mailOptions, function(err, asd) {
if (err) {
logResponse(req.connection.remoteAddress, 400, 'An error ocurred:</br>' + err, res);
return;
} else {
logResponse(req.connection.remoteAddress, 200, 'Email sent successfully', res);
return;
}
})
}
});
}
} else {
logResponse(req.connection.remoteAddress, 404, 'Incorrect request', res);
return;
}
});
server.listen(config.listenPort, config.machineIp);
console.log('Server running on http://' + config.machineIp + ':' + config.listenPort + '/');
}