📝 Added sandbox (code sketchbook)
This commit is contained in:
parent
b475595f7f
commit
b5daf18b1a
142 changed files with 100702 additions and 0 deletions
56
sandbox/email/index.js
Normal file
56
sandbox/email/index.js
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
require('dotenv').config()
|
||||
const express = require('express')
|
||||
const path = require('path')
|
||||
const bodyParser = require('body-parser')
|
||||
const nodemailer = require('nodemailer')
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.urlencoded({ extended: false }))
|
||||
app.use(express.static(path.join(__dirname, "public")))
|
||||
app.use(express.json())
|
||||
|
||||
const route = express.Router()
|
||||
const port = process.env.PORT || 8080
|
||||
|
||||
app.use('/v1', route)
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
port: 465,
|
||||
host: process.env.HOST,
|
||||
auth: {
|
||||
user: process.env.USER_EMAIL,
|
||||
pass: process.env.PASS
|
||||
},
|
||||
secure: true
|
||||
})
|
||||
|
||||
|
||||
// separate out concerns, one server serves the page, another sends the email
|
||||
route.get('/', (req, res) => {
|
||||
res.sendFile(__dirname + '/index.html')
|
||||
})
|
||||
|
||||
route.post('/text-mail', (req, res) => {
|
||||
const {subject, body} = req.body
|
||||
// create a from field in the html form
|
||||
// const {from, subject, body} = req.body
|
||||
const mailData ={
|
||||
from: process.env.FROM,
|
||||
// from: from,
|
||||
to: process.env.TO,
|
||||
subject: subject,
|
||||
text: body,
|
||||
html: `<b>Hey There!</b><br>${body}</br>`
|
||||
}
|
||||
|
||||
transporter.sendMail(mailData, function (err, info) {
|
||||
if (err) {
|
||||
return console.log(err)
|
||||
}
|
||||
res.status(200).send({ message: "Mail send", message_id: info.messageId })
|
||||
})
|
||||
})
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Server listening on port ${port}`)
|
||||
})
|
||||
1081
sandbox/email/package-lock.json
generated
Normal file
1081
sandbox/email/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
19
sandbox/email/package.json
Normal file
19
sandbox/email/package.json
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "email",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"start": "node ./index.js"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"nodemailer": "^6.8.0"
|
||||
}
|
||||
}
|
||||
43
sandbox/email/public/index.html
Normal file
43
sandbox/email/public/index.html
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<title>Basic Email Form</title>
|
||||
</head>
|
||||
<body>
|
||||
<form class="email" id="emailform">
|
||||
<div>
|
||||
<input type="text" placeholder="Subject" name="subject" required />
|
||||
</div>
|
||||
<div>
|
||||
<textarea placeholder="Your message" name="body" required></textarea>
|
||||
</div>
|
||||
<div>
|
||||
<button id="submit" >Send a message</button>
|
||||
</div>
|
||||
</form>
|
||||
<script>
|
||||
const btn = document.querySelector('#submit')
|
||||
const form = document.querySelector('#emailform')
|
||||
const url = '/v1/text-mail'
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.preventDefault()
|
||||
const formData = new FormData(form)
|
||||
let object = {}
|
||||
formData.forEach(function(value, key) {
|
||||
object[key] = value
|
||||
})
|
||||
var json = JSON.stringify(object)
|
||||
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json;charset=utf-8'
|
||||
},
|
||||
body: json
|
||||
})
|
||||
})
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue