Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript adding linebreak in mailto body

Tags:

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

like image 437
srini Avatar asked Apr 18 '12 23:04

srini


People also ask

How do I pass HTML formatted body in mailto?

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.

How do you insert a link into mailto?

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.

How do you add a new line in HTML body?

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.

How do you insert a break in 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.


1 Answers

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.

like image 99
ottomeister Avatar answered Sep 22 '22 15:09

ottomeister