Issue with mailto link in email address containing ampersand?
Asked Answered
C

1

14

I have a mailto link like that <a href="mailto:a&[email protected]" > it displays correctly on html but when we click on the link the outlook just shows a in the to address. Has anyone faced the same problem please suggest.

Cookout answered 29/2, 2012 at 11:25 Comment(2)
An ampersand is not a valid character in an E-Mail address, is it? (Edit: oh indeed, it is.) I guess percent encoding is your friend thenSaber
And mailto is very old. 90% of the visiters of your site use not their system mail, but mailservices as gmail. A link to the contactform is much userfriendlyFocus
S
32

Percent encoding the string is required for IE and I assume will work across browsers. From this MSDN document:

Windows Internet Explorer 7 and later. You must percent-encode all URL-reserved characters within a mailto: address. For example, the number sign (#) is used as a fragment identifier in URLs. When processing an address such as some#[email protected], Internet Explorer copies only the portion up to the number sign into the mail client; the fragment portion including the number sign is ignored. This behavior is by design.

So you need

 <a href="mailto:a%26b_admin%40xyz.com">

As said, I expect a percent encoded address will work in all browsers, but I don't know for sure. I can confirm it works with Chrome and Thunderbird.

Saber answered 29/2, 2012 at 11:30 Comment(4)
This line of JavaScript will convert ampersands so you don't have to do it by hand: var emailFriendlyUrl = currentUrl.replace(/&/g, '%26');.File
I my case (bookmarklet creating and opening a mailto link in Chrome) I didn't only have to encode only ampersand, but also the percent sign in front of it, which I just learned from this answer which lead me to .replace('&', '%2526')Temperate
Florian's comment solved a problem I've wrestled with for a long time. The '%2526' works, where '%26' never did.Rasorial
@Rasorial Note that %2526 is a double encoded &. encode("&") = "%26", encode("%26") = "%2526". So you may be decoding twice instead of just once.Fare

© 2022 - 2024 — McMap. All rights reserved.