136

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,988
  • 20
  • 65
  • 97

4 Answers4

239

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,574
  • 2
  • 25
  • 34
46
  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
  • 51,447
  • 27
  • 165
  • 200
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,879
  • 1
  • 22
  • 36
  • 1
    I prefer this personally. Mainly because this also works when you try and add a % – Dan Jan 09 '14 at 02:44
  • 6
    `encodeURIComponent`, rather. [See here.](http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent) – Cees Timmerman Jul 01 '14 at 14:01
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
  • 32,342
  • 7
  • 71
  • 76
  • What `include` do you use for `encodeURIComponent` ? – Frak Jul 02 '17 at 23:56
  • 1
    @frakman1 encodeURIComponent is a JavaScript function. You can use it without any includes. – kiranvj Jul 04 '17 at 05:04
  • @Koosh bcoz the above code is JavaScript, try this method to encode email body in Blazor https://stackoverflow.com/a/4550600/1188322 – kiranvj Aug 29 '20 at 04:19