Follow me on Twitter @AntonioMaio2

Tuesday, December 29, 2015

SharePoint Basics: MailTo Link with New Lines in the Email Body

I was recently asked about how to implement a MailTo link on a SharePoint site page, which is pretty straight forward.  The challenge for the person asking was about how to pre-populate the email body, so that it provided a template for users to fill out when sending that email, and how to have that email body contain multiple lines of text, with carriage returns and new lines between the lines.

There are several methods of doing this which do not work because SharePoint will actually remove the carriage returns and new lines.  There are other methods which do work. 

So, here is a quick post describing one method for accomplishing this.  I'm going to use JavaScript to setup the link so that the script is centralized in one place.

When including carriage returns and new lines in a link, we need to escape those characters so that web browsers can interpret them correctly. 
  • The carriage return is escaped with %0D
  • The new line is escaped with %0A
  • If you want to have text start on one line, then move to the next line, you need to include: %0D%0A
  • If you want to have an empty line between two lines of text, you would simply double the pattern: %0D%0A%0D%0A

In order to create a MailTo link which includes a pre-populated email subject, email addresses and body pre-populated with multiple lines, you can do the following:
  • Create a JavaScript file which contains the following:
Click <a class="email" title="My Link Title" href="#" onclick="javascript:window.location='mailto:emailaddress@company.com?subject=Here Is My Subject Line&body=Here is the start of the email body %0D%0A%0D%0A email body continuing after a new line %0D%0A%0D%0A email body continuing after a second new line %0D%0A%0D%0A email body continuing after a third new line %0D%0A%0D%0A email body continuing after a forth new line %0D%0A%0D%0A email body continuing after a fifth new line.' + window.location;">here</a> to send an email template using a predefined template.

Notice how the link title field, email subject, email addresses and email body are populated.  Notice also how the carriage return and line feed characters are included in the email body.
  • Upload the JavaScript file to the Site Assets SharePoint library
  • On the site page add a Content Editor web part
  • Edit the web part you just added
  • In the Content Link property in the web part editor, add the path to the JavaScript file that you just uploaded to your Site Assets library.
Refresh your page and test your link.

   -Enjoy.