Send Email in Sharepoint 2013 using Javascript

About

One good thing about Sharepoint is the client APIs, so you could do some impressive things using Javascript. We faced the problem to send a email to share a link, but we cannot deploy an solution, to resolve this we need to use only Javascript.

This functionality will be solved using the following code:

Send email
function sendEmail(from, to, body, subject) {
    var urlTemplate = site + "/_api/SP.Utilities.Utility.SendEmail";
    var formDigest = document.getElementById("__REQUESTDIGEST").value;
    $j.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: 'POST',
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': from,
                'To': { 'results': [to] },
                'Subject': subject,
                'Body': body
            }
        }
      ),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": formDigest
        },
        success: function (data) {
            var result = data.d.results;
            var i = result.length;
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
    });
}

We call this function like this:

Function call
sendEmail("domain\\sender", "domain\\recipient","This is the body","Hello world");

About the author

Enterprise Web Developer

Julio Mora is an experienced developer who has worked creating mobile applications for the demanding video game industry. He has experience working with international teams and has earned the respect of his team members for his commitment to delivering excellent work products. He continues to focus on building an exceptional user experience. His background includes:

  • Web Technologies: Content Management Systems (IBM WCM), JavaScript libraries (jQuery, Dojo), CSS3, HTML5, mobile web, Operating Systems.
  • Research: Artificial Intelligence, Computer Graphics and Computer Simulation for robotics.
  • Mobile Technologies : IOS Development, Android Development.