-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (68 loc) · 1.91 KB
/
index.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
require('dotenv').config()
const express = require('express')
const bodyParser= require('body-parser')
const nodemailer = require('nodemailer')
const { google } = require('googleapis')
const OAuth2 = google.auth.OAuth2
const oauth2Client = new OAuth2(
process.env.Client_ID,
process.env.Client_Secret,
"https://developers.google.com/oauthplayground"
)
oauth2Client.setCredentials({
refresh_token:process.env.Refresh_Token
})
const accessToken = oauth2Client.getAccessToken()
const app = express()
app.use(bodyParser.urlencoded({extended:true}))
app.use(bodyParser.json())
app.use(express.static('public'))
app.route("/").get(function (req, res) {
res.sendFile(process.cwd() + "/views/index.html");
});
app.use(express.static('views'))
app.post('/email',(req,response)=>{
const output=`
<p>You have a new contact request</p>
<img class="email" src="cid:email" alt="email-image">
<h3>Contact details</h3>
<ul>
<li>FirstName: ${req.body.name}</li>
<li>Subject: ${req.body.subject}</li>
<li>Email: ${req.body.email}</li>
<li>Message: ${req.body.message}</li>
</ul>`
const smtpTrans = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth:{
type:"OAuth2",
user:process.env.GMAIL_USER,
clientId:process.env.Client_ID,
clientSecret:process.env.Client_Secret,
refreshToken:process.env.Refresh_Token,
accessToken:accessToken
}})
const mailOpts = {
from:process.env.GMAIL_USER,
to:process.env.RECIPIENT,
subject:'Portfolio Website',
html:output
}
smtpTrans.sendMail(mailOpts,(error,res)=>{
if(error){
console.log(error);
}
else{
console.log("Message sent: " + res.message);
response.status(200).send('You can go back from this window now!')
}
//smtpTrans.close();
})
})
const port = process.env.PORT || 3000
const server = app.listen(port,listening)
function listening (){
console.log(`server running on ${port}`)
}