-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (54 loc) · 2.11 KB
/
app.js
File metadata and controls
62 lines (54 loc) · 2.11 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
58
59
60
61
62
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
// create reusable transporter object using the default SMTP transport
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
var smtpConfig = {
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'admin@progtive.com',
pass: 'kaka123@'
}
};
// setup e-mail data with unicode symbols
var mailOptions = {
to: 'admin@progtive.com', // list of receivers
};
router.post('/sendMessage', function(req, res) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'accept, content-type, x-parse-application-id, x-parse-rest-api-key, x-parse-session-token');
var transporter = nodemailer.createTransport(smtpConfig);
// send mail with defined transport object
if (req.body) {
mailOptions.subject = req.body.from+" - "+req.body.email;
mailOptions.text = req.body.text;
transporter.sendMail(mailOptions, function(error, info){
if(error){
res.json({"Error": console.log(error)});
}
res.json({"Message sent": info.response});
});
}else{
res.json({"empty request": req.body});
}
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);