-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
65 lines (56 loc) · 1.52 KB
/
express.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
var express = require('express');
var router = express.Router();
var nodemailer = require('nodemailer');
//var smtpTransport = require('nodemailer-smtp-transport');
var cors = require('cors');
const creds = require('./config');
////GMAIL SMTP
nodemailer.createTestAccount((err, account) => {
let transporter = nodemailer.createTransport({
host: 'smtp.googlemail.com', // Gmail Host
port: 465, // Port
secure: true, // this is true as port is 465
auth: {
///In the config.js, please add your mail and password
user: creds.USER,
pass: creds.PASS
}
});
// var transporter = nodemailer.createTransport(transport)
transporter.verify((error, success) => {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take messages');
}
});
router.post('/send', (req, res, next) => {
var name = req.body.name
var email = req.body.email
var message = req.body.message
var content = `name: ${name} \n email: ${email} \n message: ${message} `
var mail = {
from: name,
///your mail recipient
to: 'Your email',
subject: 'New Message from Contact Form',
text: content
}
transporter.sendMail(mail, (err, data) => {
if (err) {
res.json({
status: 'fail'
})
} else {
res.json({
status: 'success'
})
}
})
})
const app = express()
app.use(cors())
app.use(express.json())
app.use('/', router)
app.listen(3002)
});