-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (54 loc) · 1.75 KB
/
Copy pathapp.js
File metadata and controls
65 lines (54 loc) · 1.75 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
63
64
65
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var port = 3000;
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/',function(req,res){
res.render('index', {title: 'Home'});
});
app.get('/about',function(req,res){
res.render('about', {title: 'About Us'});
});
app.get('/contact',function(req,res){
res.render('contact');
});
//Post request using nodemailer to send the message
app.post('/contact/send',function(req,res){
console.log('Sending Email');
//Change host and port to the correct service provider's details
var transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: '465',
secure: true,
//Credentials go here
auth: {
user: 'email@example.com',
pass: 'password'
}
});
//From field must be the same as the user set up in auth field above.
var mailOptions = {
from: 'John <john@example.com',
to: 'Doe <doe@example.com>',
subject: 'Website Contact',
text: 'You have a new contact submission:.\nName: ' + req.body.name + '\nEmail: ' + req.body.email + '\nMessage: ' + req.body.message,
html: '<p>You have a new contact submission</p><ul><li>Name: '+req.body.name+'</li><li>Email: '+req.body.email+'</li><li>Message: '+req.body.message+'</li></ul>'
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
res.redirect('/');
} else {
console.log('Message Sent: '+info.response);
res.redirect('/');
}
});
});
app.listen(port);
console.log("Server running on port " + port);