-
Notifications
You must be signed in to change notification settings - Fork 0
/
form2mailto.js
27 lines (18 loc) · 1007 Bytes
/
form2mailto.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
$o('form *[type=submit]').addEventListener('click', function(evt) {
evt.preventDefault()
alert('Now, it will open your email client, for you to send a message?')
const form = evt.target.closest('form')
// fetches email from form's html "data-email" attribute
const sendToEmail = form.dataset.email
// change this accordingly or fit this selectors to your html form code
const email = form.querySelector('input[type=email]').value
const subject = form.querySelector('*[name=_subject]').value
const name = form.querySelector('input[name=name]').value
const text = form.querySelector('textarea[name=message]').value
// new line separator for email body
const nl = '\r\n'
const message = text + nl + nl + '---' + nl + name
const mailtoLink = 'mailto:' + sendToEmail + '?body=' + encodeURIComponent(message) + '&subject=' + encodeURIComponent(subject)
// trigger mailto link
window.open(mailtoLink)
});