Icon: News & Updates News & Updates
Fast just got faster! We are very proud to announce the release of Spirit 5.3.
Welcome to our new range of small business web site packages.
Icon: Clients & Partners Clients & Partners

News & Articles

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;
}