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

Grow your business

Find out how Deep Blue Sky can grow your business.

  1. Digital benchmark
  2. Digital roadmap
  3. Digital engineering

Write a comment.

Responses. (5)

  1. THANK YOU!

    Thanks  A LOT !!!

    After trying SEVERAL tips from multiple sources, FINALLY your example WORKED.

    Great Job !

     

     

  2. Add more lines

     <script language="javascript">

    function mailpage()

    {

    mail_str = "mailto:?subject=Webpage xxx " + document.title;

    mail_str += "&body=I found...." + escape ("\n\n") + document.title;

    mail_str += escape ("\n\n") + "You can find this information on the following location: " + escape ("\n\n") + location.href;

    location.href = mail_str;

    }

    </script>

  3. I P

    Ian

    mailpage

    Thanks, Jim, for saving me hours of experimentation!

  4. M M

    Melissa

    Only useful formatting tutorial on the internet

    Thanks so much for this. After hours of searching, this was the only viable, straightforeward tutorial on how to insert hard returns I was able to find.

  5. F M

    F

    doesn't work anymore

    Does not seem to work in new browsers.