mailapi/index.html.sample

68 lines
2.4 KiB
Plaintext

<p>You may use this web form to contact us. Your message will be encrypted. A copy of your message will not be sent to you unless we reply. For an encrypted reply, paste or link your public key. </p>
<noscript><p>Uh-oh, you don't have javascript. This form won't work without it. Please send an email instead.</p></noscript>
<p><input type="text" id="name-input" placeholder="Your name"></p>
<p><input type="email" id="email-input" placeholder="Your email"></p>
<p><input type="text" id="subject-input" placeholder="Subject"></p>
<p><textarea id="message-input" placeholder="Your message"></textarea></p>
<p><button id="send-button">Send</button></p>
<script src="/PATH/TO/LOCAL/COPY/OF/openpgp.min.js"></script>
<script>/* global openpgp fetch */
let send = document.getElementById('send-button')
let name = document.getElementById('name-input')
let email = document.getElementById('email-input')
let subj = document.getElementById('subject-input')
let text = document.getElementById('message-input')
const API_URL = "https://mailapi.mydomain.tld/"
async function sendClicked () {
send.disabled = true
send.innerHTML = `Sending... `
let res; try {
res = await fetch(API_URL, {
method: 'POST',
// cache: 'no-cache',
headers: {'content-type': 'application/json'},
body: JSON.stringify({
name: name.value,
subj: subj.value,
email: email.value,
msg: await openpgp.encrypt({
message: await openpgp.createMessage(
{ text: `${text.value}\n` }
),
encryptionKeys: await openpgp.readKey({
armoredKey: `-----BEGIN PGP PUBLIC KEY BLOCK-----
mQGNBF/TtIoBDADvYLnftyJjfWoeK0zE3Yh3jYsuAj27aU039xh6VaX0IsXQqKLD
...
lk6lY0ktTb+vRnndyN3m+XW1mYdv3xUZMjQwMBtgdZbfY43pq8+N55tSTycF
=Wvbt
-----END PGP PUBLIC KEY BLOCK-----`,
}),
}),
}),
})
} catch (err) {
send.disabled = false
console.error(err)
alert('Failed to connect to the network. Are you online?')
}
console.log(res.json())
send.disabled = false
if (res.status===200) {
text.value = ''; subj.value = ''; name.value = ''; email.value = ''
alert(Sent!')
} else if (res.status===500)
alert('Backend failed! Please try again. If the problem persists, please email hostmaster@[this domain].')
else alert('Unknown error! Please try again. If the problem persists, please email hostmaster@[this domain].')
}
</script>