mailto link multiple body lines
Asked Answered
W

4

138

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 protected]?&subject=test&body=type%20your&body=message%20here

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

thoughts?

Winker answered 27/4, 2012 at 18:56 Comment(0)
R
241

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

mailto:[email protected]?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.

Rios answered 27/4, 2012 at 19:4 Comment(6)
Thanks, this worked well for me and seems much simpler than other solutions floating around on the internet.Spectacles
This should work for all special characters, right? &=%26, %=%25, are there any characters where this pattern doesn't hold?Melaniemelanin
for info, $0A is simply escape( "\n" )Tubb
That encoder uses the standard encodeURIComponent to encode the URI components.Emalee
%0D%0A Worked for me for email and share in whatsapp messagePilch
I've been trying to use this to do a double line break, whenever I use 2 %0D%0A together it breaks the hyperlinkChemesh
C
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:[email protected]?body=send%20current-issue%0D%0Asend%20index>

The above mailto body corresponds to:

send current-issue
send index
Cotswold answered 12/12, 2014 at 15:49 Comment(0)
J
20

To get body lines use escape()

body_line =  escape("\n");

so

href = "mailto:[email protected]?body=hello,"+body_line+"I like this.";
Jesseniajessey answered 4/10, 2013 at 11:41 Comment(2)
I prefer this personally. Mainly because this also works when you try and add a %Gurias
encodeURIComponent, rather. See here.Emalee
S
18

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:[email protected]?body=" + emailBody;

Check encodeURIComponent docs

Stateroom answered 3/10, 2014 at 16:59 Comment(3)
What include do you use for encodeURIComponent ?African
@frakman1 encodeURIComponent is a JavaScript function. You can use it without any includes.Stateroom
@Koosh bcoz the above code is JavaScript, try this method to encode email body in Blazor https://mcmap.net/q/111914/-does-c-have-an-equivalent-to-javascript-39-s-encodeuricomponentStateroom

© 2022 - 2024 — McMap. All rights reserved.