Carriage Returns, Line Feeds and Ampersands in IE / Outlook mailto: links

I do quite a lot of work on Intranets, on which the only browser in use is Microsoft Internet Explorer. This often means that IE-specific tricks can be used safely on the Intranet. This specific trick is for using IE with Outlook.

I read on MSDN about how to include line breaks in the body of an email created via a mailto: link, when opening the mailto using Outlook Express (and Outlook).

However, this was causing some problems for users with IE5.0. Turns out there’s a problem in IE 5.0 with this approach. IE 5.0 doesn’t correctly translate these line breaks into Outlook.

The solution, for IE5, is to qualify your carriage returns or line feeds with the %25 escape character, thus:

<A HREF="mailto:email1;email2&cc=email3?subject=My%20Subject&body=Line1%250DLine2"> Click Here To Invoke Mail </A>

But there’s a snag. IE5.5 fixes the problem, and moreover the escaping no longer works. So whilst the code above will make IE5 happy, IE5.5 or later will complain.

Although it’s not mentioned in the MSDN documents, I also found a problem when passing an encoded ampersand in a mailto: link. The solution is the same – prepend %25 for IE5.0, don’t prepend it anywhere else.

The solution, therefore, is to check which version of IE you are using in advance, and set the breaks accordingly. I used some code adapted from the Ultimate Javascript Client Sniffer:


// get the user agent (lowercase to make comparisons easier)
var agt = navigator.userAgent.toLowerCase();
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3 = (is_ie && (is_major < 4));
var is_ie4 = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up = (is_ie && (is_major >= 4));
var is_ie5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie6 = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

if (is_ie5_5 || is_ie6up) {
var EncodedLineBreak = "%0D";
var EncodedLineFeed = "%0A";
var EncodedAmpersand = "%26";
} else {
var EncodedLineBreak = "%250D";
var EncodedLineFeed = "%250A";
var EncodedAmpersand = "%2526";
}

Note: This approach hasn’t been tested with IE4 or earlier, or with IE7, or Outlook Express. It’s been tried successfully with IE5, IE5.5, and IE6, alongside Outlook 2002.

2 thoughts on “Carriage Returns, Line Feeds and Ampersands in IE / Outlook mailto: links

  1. Thanks for this – I used the technique to get an email this page link to retain the variables that were separated by ampersands. My script now passed the full page address across to email programs rather than stopping at the first ‘&’

  2. Hi Steve,

    That’s a neat idea – so regardless of what the page is, or what its query string might be, you can still pass it into the body of an email with its query string intact.

    Out of interest, have you run into any problems with maximum query string lengths in any browsers? Given that your mailto link will always be longer than the URL, what would do you if the URL was approaching the max size allowed?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>