mailto link multiple body lines

107,276

Solution 1

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.

Solution 2

  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

Solution 3

To get body lines use escape()

body_line =  escape("\n");

so

href = "mailto:[email protected]?body=hello,"+body_line+"I like this.";

Solution 4

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

Share:
107,276
KevinDeus
Author by

KevinDeus

C# JQuery nunit WCF Subversion SharpSVN TFS RoombaSCI CreateOI Framework ChessMangler

Updated on November 16, 2020

Comments

  • KevinDeus
    KevinDeus over 3 years

    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?

  • bryn
    bryn about 11 years
    Thanks, this worked well for me and seems much simpler than other solutions floating around on the internet.
  • blast_hardcheese
    blast_hardcheese over 10 years
    This should work for all special characters, right? &=%26, %=%25, are there any characters where this pattern doesn't hold?
  • Dan James Palmer
    Dan James Palmer over 10 years
    I prefer this personally. Mainly because this also works when you try and add a %
  • divillysausages
    divillysausages almost 10 years
    for info, $0A is simply escape( "\n" )
  • Cees Timmerman
    Cees Timmerman almost 10 years
    encodeURIComponent, rather. See here.
  • Cees Timmerman
    Cees Timmerman almost 10 years
    That encoder uses the standard encodeURIComponent to encode the URI components.
  • BenB
    BenB almost 8 years
    %0D%0A Worked for me for email and share in whatsapp message
  • frakman1
    frakman1 almost 7 years
    What include do you use for encodeURIComponent ?
  • kiranvj
    kiranvj almost 7 years
    @frakman1 encodeURIComponent is a JavaScript function. You can use it without any includes.
  • Fiztban
    Fiztban almost 6 years
    I've been trying to use this to do a double line break, whenever I use 2 %0D%0A together it breaks the hyperlink
  • kiranvj
    kiranvj over 3 years
    @Koosh bcoz the above code is JavaScript, try this method to encode email body in Blazor stackoverflow.com/a/4550600/1188322