As David notes, his suggestion does not actually fulfill the OP's request, which was an email with subject and message. It doesn't work because most, maybe all, combinations of browsers plus e-mail clients do not accept the subject
and body
attributes of the mailto:
URI when supplied as a <form>
's action
.
But here's a working example:
HTML (with Bootstrap styles):
<p><input id="subject" type="text" placeholder="type your subject here"
class="form-control"></p>
<p><input id="message" type="text" placeholder="type your message here"
class="form-control"></p>
<p><a id="mail-link" class="btn btn-primary">Create email</a></p>
JavaScript (with jQuery):
<script type="text/javascript">
function loadEvents() {
var mailString;
function updateMailString() {
mailString = '?subject=' + encodeURIComponent($('#subject').val())
+ '&body=' + encodeURIComponent($('#message').val());
$('#mail-link').attr('href', 'mailto:[email protected]' + mailString);
}
$( "#subject" ).focusout(function() { updateMailString(); });
$( "#message" ).focusout(function() { updateMailString(); });
updateMailString();
}
</script>
Notes:
<form>
element with associated action
attribute is not used.<input>
element of type button
is also not used.
<a>
styled as a button (here using Bootstrap) replaces <input type="button">
focusout()
with updateMailString()
is necessary because the <a>
tag's href
attribute does not automatically update when the input fields' values change.updateMailString()
is also called when document is loaded in case the input fields are prepopulated.encodeURIComponent()
is used to get characters such as the quotation mark (") across to Outlook.In this approach, the mailto:
URI is supplied (with subject
and body
attributes) in an a
element's href
tag. This works in all combinations of browsers and e-mail clients I have tested, which are recent (2015) versions of:
Bonus tip: In my use cases, I add some contextual text to the e-mail body
. More often than not, I want that text to contain line breaks. %0D%0A
(carriage return and linefeed) works in my tests.