forked from techinems/mailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (47 loc) · 1.69 KB
/
server.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
const express = require('express');
require('dotenv').config();
const nodemailer = require('nodemailer');
const service_account = require('./keys/gmail_service_creds.json');
const PORT = process.env.PORT || 3000;
const WEBSITE_VERIFICATION_TOKEN = process.env.WEBSITE_VERIFICATION_TOKEN;
const FROM_EMAIL = process.env.FROM_EMAIL;
const FROM_NAME = process.env.FROM_NAME;
const REPLY_TO_EMAIL = process.env.REPLY_TO_EMAIL;
const HOMEPAGE = process.env.HOMEPAGE;
// Initializes express app
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.post('/sendmail', async(req, res) => {
if (req.body.token !== WEBSITE_VERIFICATION_TOKEN) {
return res.redirect(HOMEPAGE);
}
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: FROM_EMAIL,
serviceClient: service_account.client_id,
privateKey: service_account.private_key
}
});
let success = true;
try {
await transporter.sendMail({
from: `"${FROM_NAME}" <${FROM_EMAIL}>`,
replyTo: REPLY_TO_EMAIL,
to: req.body.to,
subject: req.body.subject,
text: req.body.body,
// html: '<b> Test </b>'
});
res.send({ success: true, msg: 'Email successfully sent!' });
} catch (err) {
console.error(err);
res.send({ success: false, msg: err });
}
});
app.get('/', (req, res) => res.redirect(HOMEPAGE));
app.listen(PORT, () => console.log(`App listening on Port: ${PORT}`));