mailapi/index.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-11-24 11:02:52 -07:00
'use strict'
2021-11-24 11:19:34 -07:00
require('dotenv').config()
const express = require('express')
2021-11-24 11:19:34 -07:00
const {verify} = require('hcaptcha')
2021-11-24 12:37:26 -07:00
const mailer = require('nodemailer').createTransport({
host: process.env.MAIL_SERVER,
port: 587,
secure: false, // STARTTLS LATER
auth: {
user: process.env.MAIL_USER,
pass: process.env.MAIL_PASS,
},
})
express().use(express.json())
2021-11-24 13:52:04 -07:00
.get('/', (req, res) => {
res.send('Hello')
}
.post('/', async (req, res) => {
2021-11-24 11:19:34 -07:00
// Check token
try {
const data = await verify(process.env.HCAPTCHA_SECRET, req.body['token'])
} catch (err) { console.error(err) }
2021-11-24 12:37:26 -07:00
if (data.success === true) {
let from
if (req.body['name'] && req.body['email']) from = `${req.body['name']} <${req.body['email']}>`
else if (req.body['name']) from = req.body['name']
else if (req.body['email']) from = req.body['email']
else from = 'Anonymous'
2021-11-24 11:02:52 -07:00
2021-11-24 12:37:26 -07:00
// Send email
try {
await mailer.sendMail({
from: from,
to: process.env.MAIL_TO,
subject: req.body['subj'],
text: req.body['msg'],
})
} catch (err) { console.error(err) }
}
}).listen(process.env.PORT, () => {
2021-11-24 11:02:52 -07:00
console.log(`API started`)
})