11

I have mailto button in all pages of my site and I want to write reference to that page in e-mail body.

In one page I can have

<a class="email" title="Email a friend" href="mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0ahttp://www.a.com/Home/SiteMap/tabid/589/Default.aspx">Email</a>

But how can I make this generic for all pages?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • Javascript could do this for you. By using the "document.location.href". Or, if you are using .php you can do it even easier. Can you use php? let me know and I can make an example for you! – Graeme Leighfield Nov 02 '11 at 07:55
  • No, I use Asp ;) But how can I wrote javascript in that part?(body="...") – Chuck Norris Nov 02 '11 at 07:59

4 Answers4

36

This is the pure javascript solution:

<a class="email" title="Email a friend" href="#" onclick="javascript:window.location='mailto:?subject=Interesting information&body=I thought you might find this information interesting: ' + window.location;">Email</a>
reticent
  • 1,669
  • 1
  • 14
  • 22
9

With Javascript, you utf-8-percent-encode the subject and body hfvalues using encodeURIComponent() on a UTF-8 page.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title></title>
        <script>
            function SendLinkByMail(href) {
                var subject= "Interesting Information";
                var body = "I thought you might find this information interesting:\r\n\r\n<";
                body += window.location.href;
                body += ">";
                var uri = "mailto:?subject=";
                uri += encodeURIComponent(subject);
                uri += "&body=";
                uri += encodeURIComponent(body);
                window.open(uri);
            }
        </script>
    </head>
    <body>
        <p><a href="javascript:(function()%7BSendLinkByMail()%3B%7D)()%3B">Email link to this page</a></p>
    </body>
</html>

If you're doing this server-side, you can just build the mailto link and emit it as the href attribute value. Then, you won't need JS at all.

I assume ASP has some URI encoding functions that work like encodeURIComponent().

You can also view the source of my mailto URI composer page as another example.

You an also take a look at http://shadow2531.com/opera/testcases/mailto/mailto_uri_scheme_idea.html#send_link_by_mail and my mailto URI syntax validator.

For the < and > that I encase the URI in, in the JS code above, see "Appendix C. Delimiting a URI in Context" of RFC3986 for why.

Also, instead of window.location.href, you can use window.location or document.location.href or document.location. I normally use "document.location".

For why you use a Javascript URI instead of an onclick attribute, see this answer.

Also note that in the JS URI in the code above, I wrapped the code in an anonymous function. That's not necessary in this case because the function doesn't return anything that would change the document when you click. But, it's to just do it all the time for good measure.

See my Javascript URI compose to help with creating javascript URIs.

Community
  • 1
  • 1
Shadow2531
  • 11,980
  • 5
  • 35
  • 48
4

You can put a JS on every page that can modify all the hrefs of all email link when the document loads.

I'll use jQuery for the example code because is more compact, but this can be achieved also with pure JS:

$(document).ready(function(){
    $(".email").attr("href", "mailto:?subject=Interesting%20information&amp;body=I thought you might find this information interesting:%20%0d%0a"+window.location);
});
Nicu Surdu
  • 8,172
  • 9
  • 68
  • 108
  • 1
    of course it can be achieved pure javascript :), what is jquery? – david Nov 02 '11 at 08:21
  • 2
    @david: "jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript." – Nicu Surdu Nov 02 '11 at 08:36
  • thanks nick, i was just having a dig at you :} `what is jquery` javascript – david Nov 02 '11 at 08:43
  • I have used your code, first body is't pass to email b'coz "&" when I changed to "&" it's work. at the end your idea is good to pass current link with JQuery :) thank you. – Ghanshyam Gohel Jan 07 '16 at 19:49
1

you need to use &body= to include a string in the email body

here is a generic javascript function

 <script>
   function emailFriend(){

   var strrep ,ptitle = document.title;

strrep= ptitle.replace(/"/g,'%22');
strrep= ptitle.replace(/&/g,'%26');

var mailtourl = "mailto:?subject=Interesting%20information&body=I thought you might find this information interesting: "+encodeURIComponent(location.href);
location.href = mailtourl;
return false
}

 </script>


<a href="javascript:;" onclick="emailFriend();return false">Email</a>
david
  • 4,218
  • 3
  • 23
  • 25