130

having trouble getting multiple lines to work correctly in a mailto link

In my case I'm testing it with an Outlook default mail reader.

The following is put in an anchor href:

mailto:email@address.com?&subject=test&body=type%20your&body=message%20here

only "message here" shows up in the email body. (whether I use chrome or IE)

thoughts?

KevinDeus
  • 11,610
  • 20
  • 62
  • 96

4 Answers4

227

You can use URL encoding to encode the newline as %0A.

mailto:email@address.com?subject=test&body=type%20your%0Amessage%20here

While the above appears to work in many cases, user olibre points out that the RFC governing the mailto URI scheme specifies that %0D%0A (carriage return + line feed) should be used instead of %0A (line feed). See also: Newline Representations.

Community
  • 1
  • 1
cyang
  • 5,184
  • 1
  • 23
  • 34
45
  1. Use a single body parameter within the mailto string
  2. Use %0D%0A as newline

The mailto URI Scheme is specified by by RFC2368 (July 1998) and RFC6068 (October 2010).
Below is an extract of section 5 of this last RFC:

[...] line breaks in the body of a message MUST be encoded with "%0D%0A".
Implementations MAY add a final line break to the body of a message even if there is no trailing "%0D%0A" in the body [...]

See also in section 6 the example from the same RFC:

<mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index>

The above mailto body corresponds to:

send current-issue
send index
Community
  • 1
  • 1
oHo
  • 46,029
  • 27
  • 151
  • 189
20

To get body lines use escape()

body_line =  escape("\n");

so

href = "mailto:me@my.com?body=hello,"+body_line+"I like this.";
lokeshjain2008
  • 1,839
  • 1
  • 22
  • 34
17

This is what I do, just add \n and use encodeURIComponent

Example

var emailBody = "1st line.\n 2nd line \n 3rd line";

emailBody = encodeURIComponent(emailBody);

href = "mailto:me@somesite.com?body=" + emailBody;

Check encodeURIComponent docs

kiranvj
  • 27,729
  • 5
  • 65
  • 72