Creating mailto links in SharePoint with newlines / carriage returns

Standard

Creating a mailto: link is usually easy. You just generate a string in the form, e.g.:

email@address.com?subject=sub&body=bodytext

where sub and body are URL encoded strings. The URL encoding means, for example, you change spaces to %20 and newlines to %0A%0D, among others.

The problem with SharePoint is that if you paste the escaped string into the HTML view of a content editor web part, SharePoint, in all it’s wisdom, Converts the escaped strings back into normal text, which means all the newlines (and multiple spaces) are stripped.

This makes it seemingly impossible to create a mailto link with newlines.

Here’s the solution:

The solution is to use JavaScript instead, and paste the lines unescaped into the script. Then make a basic mailto: link as before, but give it an id (in this case, the id is mailtolink). Then the mailto link is overwritted with our desired link, complete with newlines, when the page loads.

<p>Please <a href="mailto:address@example.com" id="mailtolink">contact us</a>.</p>


<script type="text/javascript">
// <![CDATA[

var msgRec = 'address@example.com';
var msgSub = 'This is the message subject';
var msgBody = 'This is the full e-mail body\n\nThis is a new line\nAnd another line';

function modifyMailtoLink;
	document.getElementById('mailtolink').href = 'mailto:' + encodeURIComponent(msgRec) + '?subject=' + msgSub + '&body=' + encodeURIComponent(msgBody);
};

_spBodyOnLoadFunctionNames.push('modifyMailtoLink');

// ]]>
</script>

51 thoughts on “Creating mailto links in SharePoint with newlines / carriage returns

  1. Hey There. I discovered your blog the usage of msn. This
    is a really well written article. I’ll be sure to bookmark it and come back to learn extra
    of your helpful information. Thank you for the post. I’ll
    definitely comeback.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>