Sending email with SharePoint and jQuery

Want to send email from SharePoint using jQuery? No problem!

No need to create workflows or set up SMTP servers, just use this simple function:

function sendEmail(from, to, body, subject) {

var siteurl = _spPageContextInfo.webServerRelativeUrl;

var urlTemplate = siteurl + "/_api/SP.Utilities.Utility.SendEmail";

$.ajax({

   contentType: 'application/json',

   url: urlTemplate,

   type: "POST",

   data: JSON.stringify({

       'properties': {

           '__metadata': { 'type': 'SP.Utilities.EmailProperties' },

           'From': from,

           'To': { 'results': [to] },

           'Body': body,

           'Subject': subject

       }

   }

 ),

   headers: {

       "Accept": "application/json;odata=verbose",

       "content-type": "application/json;odata=verbose",

       "X-RequestDigest": $("#__REQUESTDIGEST").val()

   },

   success: function (data) {

      alert("Eposten ble sendt");

   },

   error: function (err) {

       alert(err.responseText);

       debugger;

   }

});

}

This works nicely on SharePoint online, apparently it works on OnPremise as well. Just note that the recipient is limited to a valid SharePoint user for security reasons.

This article is part of the GWB Archives. Original Author: Thorvald Bøe

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.