Adding line breaks to mailto links
Ever wanted to format mailto emails with line breaks or other formatting rules? We explain how.
Recently a client specified that they'd like users to have the ability to email a pages from their website. I therefore went ahead and used the following Javascript function to create a HTML mailto:
function mailpage() {
mail_str = "mailto:?subject=I found a website that might interest you.";
mail_str += "&body=" + document.title "This might interest you too, " + location.href;
location.href = mail_str;
}
Unfortunately the body text of the email then displays on one line, which as one client remarked looked "...a little ungainly". Therefore we set to work to find a solution and discovered/remembered this - the Javascript escape() method. Basically this accepts a string to be converted into url safe encoding. So all you have to do to see line breaks in your email is add "\n\n" and the rest is done for you. Finished code below:
function mailpage() {
mail_str = "mailto:?subject=I found a website that might interest you.";
mail_str += "&body=" + escape( document.title+"\n\nThis might interest you too, " + location.href );
location.href = mail_str;
}
2 Comments on this post
Speak to an Expert now on 01225 444 674
Talk to Us
said “THANK YOU!” years ago
said “Add more lines” last year