Skip to content

Instantly share code, notes, and snippets.

@abdus
Created November 7, 2019 08:33
Show Gist options
  • Save abdus/110a9c5cc16a52aea43dab4c1ac6f586 to your computer and use it in GitHub Desktop.
Save abdus/110a9c5cc16a52aea43dab4c1ac6f586 to your computer and use it in GitHub Desktop.
Sending Encrypted Emails from Front-End/Static Sites
<html>
<body>
<form action="">
<input
type="email"
id="email"
name="email"
placeholder="[email protected]"
/>
<br />
<input type="text" id="name" name="name" placeholder="Abdus" /> <br />
<textarea
name="message"
id="message"
cols="30"
rows="10"
placeholder="Your awesome message"
></textarea
><br />
<input type="submit" value="Encrypt and Send" />
</form>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/openpgp/4.6.2/openpgp.min.js"
integrity="sha256-txcrHjb7Yt1zrebRnGkk5OwDkoBrvJTjvJx5b7qBtWc="
crossorigin="anonymous"
></script>
<script>
const public_key = `-----BEGIN PGP PUBLIC KEY BLOCK-----
mQINBF29Z7sBEAC+qSHbQO7DUOLeqRQ5M0u+UqOZGyjAW9k3CdOb/Gzw42qXxVlD
h6xxH6IWpGMlNnczTMzRQdGP+3YFot13QpnmyuXdl0hpKIF9cH3dWH2aHLOz6P+c
i2MHO7GnyN7W84mDhCe0sVq7yBlWhg1FYUWAPZpwEFbMwCqJpkiECgY7yzdhUggF
otZ9r/UafqLYiPk7uPmtKUsugZt6MGIKNTifIxaPPFlV29O3hocU0wUOD6xqSp9Y
p6UJ8GLyH2gFeBrHTV6N1glKDhmLfXyaPeEbsd+FOUE0/0GQJmI9bj+AYBQwiw/Y
nv8OBNub/dNZS5UY6Fd3uHr5pdWMamZaCwAV09rjBwCYfqf+vXkM6/vuJG0xihpb
9CVPvE0c9mrd0IbqTHplOwDarSh94PvaGuwnQhVX9HCjFnEnC0yQcskjEfhMK9G4
xEVZjiBUSWYOEJ6pjpFH2GUFU2cN/DqyabtxillDFpYV5snGwRXTd8cyXNzunc0F
LKdZIp3FNFpvLI6gDL4gxhI93HyS2Q2FY2jiqvuEEHZ+4I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CXxHHz60aP55108U9zpEXRP0L3lqqZMkAmc
j6PgBh7ctz8lr6+p8ZeEdTeEkdOGRUBD8H12i12fAHrf7Wy5Keo86RphhwmQVamD
7UPdJVuYMqBOweglKNTf6I6LhUFCS3fJmg2Ws4V4ZCKL6ctHCr2+T1636CI3PUhr
iTV/engzn7YvqDf+VSkT/jznHsiV7gL0dvoT+YabyycXL4fV8b22QsDcY6jV2PAf
n43dNQ0Eq5Gplr2hvMbN+j+L1/327kpZNhqRKaBp92oTNdIwzJbE80JdvIvjqEO5
llNZTlYl0ur5KnWm0Ayyma0gk3f8IJG6ftu3QlqrSZIcd9PQx139+U01Qv2j/sD5
g1iMMn4KftsA4vY3TXlE9PUfXGiElcxj2XjX1/VShkj84pK4w6Go3xs1AV5D1L2Y
RtCabQ/RXmQWlHotRu0+MtynJCdFnnkctIHnxUBn3/8=
=YccN
-----END PGP PUBLIC KEY BLOCK-----
`;
async function encrypt(message) {
const enc_message = await openpgp.encrypt({
message: openpgp.message.fromText(message),
armor: true,
publicKeys: (await openpgp.key.readArmored(public_key)).keys,
});
sendMail(enc_message.data);
}
document.querySelector('form').addEventListener('submit', e => {
// dom manipulation
const sender = {
name: document.querySelector('#name').value,
email: document.querySelector('#email').value,
message: document.querySelector('#message').value,
};
// prevent page from reload on form submit
e.preventDefault();
// finally call our encrypt function
encrypt(`${sender.name} <${sender.email}> \n\n\n ${sender.message}`);
});
function sendMail(encrypted_message) {
fetch('https://formspree.io/xyyyywgd', {
method: 'POST',
mode: 'no-cors',
body: JSON.stringify({
message: encrypted_message,
}),
})
.then(console.log)
.catch(console.log);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment