I'm setting the body of an email using values from a form
firstname = bob lastname = dole ebody = 'First Name: ' + firstname + '\r\n' + 'Last Name: ' + lastname window.location.href = 'mailto:[email protected]?subject=test email&body=' + ebody;
If I do an "alert(ebody);" I get the linebreak between firstname & lastname, however when it opens up outlook, the entire ebody string appears without a linebreak in the email body.
I've tried just \n also. is there something that can give be a line break?
Thanks in advance
The Mailto format does not support HTML code emails. Outlook was used at 2003, but to become compliant with the mailto: standard they removed that functionality. But you can Use %0D%0A for a line break in HTML body.
On the Insert tab, click Link or Hyperlink. Under Link to, click E-mail Address. Either type the email address that you want in the E-mail address box, or select an email address in the Recently used e-mail addresses list. If you want to change the link text, in the Text to display box, type the text.
To add a line break to your HTML code, you use the <br> tag. The <br> tag does not have an end tag. You can also add additional lines between paragraphs by using the <br> tags. Each <br> tag you enter creates another blank line.
To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.
RFC 2368 says that mailto body content must be URL-encoded, using the %-escaped form for characters that would normally be encoded in a URL. Those characters includes spaces and (as called out explicitly in section 5 of 2368) CR and LF.
You could do this by writing
ebody = 'First%20Name:%20' + firstname + '%0D%0A' + 'Last%20Name:%20' + lastname;
but it's easier and better to have JavaScript do the escaping for you, like this:
ebody = 'First Name: ' + firstname + '\r\n' + 'Last Name: ' + lastname; ebody = encodeURIComponent(ebody);
Not only will that save you from having to identify and look up the hex values of characters that need to be encoded in your fixed text, it will also encode any goofy characters in the firstname
and lastname
variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With