-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (50 loc) · 1.36 KB
/
app.js
File metadata and controls
58 lines (50 loc) · 1.36 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
require('dotenv').config()
const express = require('express')
const nodemailer = require('nodemailer')
const bodyParser = require('body-parser')
const cors = require('cors')
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
const port = 3000
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth:{
user: process.env.MY_EMAIL,
pass: process.env.MY_EMAIL_PASSWORD
}
})
app.get('/', (req,res) => {
res.send("Hello Iklas!")
})
app.post('/send', (req,res)=> {
console.log('Sending email . . .')
const { name, to, subject, text } = req.body
const mailOptions = {
from: process.env.MY_EMAIL,
to,
subject,
text
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
res.status(400).json({
message : 'Failed sending message!',
description : error.response,
})
} else {
console.log('Email sent: ' + info.response);
res.status(200).json({
message: 'Success sending message!',
description: info.response,
})
}
});
})
app.listen(port, () => {
console.log(`App running on http://localhost:${port}`)
})