Sending an Email in SharePoint 2013 via Javascript (REST API)

I was looking for a way to send an email in SharePoint that didn’t require a compiled solution, since that usually involved scheduled maintenance windows.  I stumbled upon “SP.Utilities.Utility.SendEmail” in my searches.  This is a super simple way to get an email sent, with minimal hassle.

There really is only one catch here. The email recipient(s) must be users that have visited the site.  (ie. company email accounts).  Random email accounts that have not been authenticated to SharePoint will throw an error.

I will also note, that if you get a 403 Forbidden error, the problem could be one of two things.

  1. You should first check to make sure you are getting the digest. A simple “alert” should let you know.  If you are not getting the digest (ie. maybe your form isn’t in a SP masterpage), try using Scot Hillier’s “Wingtip” helper to grab it: http://www.shillier.com/archive/2013/09/08/managing-sharepoint-2013-app-context.aspx
  2. If you are getting the digest, it could be the same issue I ran into. I had an older version of jQuery (1.4.2).  Upgrading to a newer version (1.11) solved the 403 issue for me.

The HTML can be added to a simple Content Editor Web Part, or elsewhere…

​​Full Name: ​<br/>
<input type="text" name="fname" id="fname" placeholder="Your Full Name"/><br/> 
Your Email: <br/>
<input type="text" name="femail" id="femail" placeholder="Your Email Address"/><br/>
Your Message: <br/>
<textarea rows="4" cols="50" id="fmessage"></textarea><br/><br/>
<button type="button" id="fbutton">Send Message</button> 
<script type="text/javascript" src="jquery.js"></script>​​​​​​​​​​​​ 
<script type="text/javascript" src="emailform.js"></script> ​​​​​​​​​​​​​​

emailform.js code:

$(document).ready(function() {
    $("#fbutton").click(function(){
       var siteurl = _spPageContextInfo.webServerRelativeUrl;
       var name = $("#fname").val();
       var from = $("#femail").val();
       var msg = 'From: ' + name + '<br/><br/>' + 'Email: ' + from + '<br/><br/><br/>' + $("#fmessage").val();

       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': ['jdoe@company.com'] },
              'Body': msg,
              'Subject':'New Message From SharePointWebsite'
             }
           }),
         headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()                 
         },
         success: function (data) {
           alert('Your message has been sent');
           $("#fname").val('');
           $("#femail").val('');
           $("#fmessage").val('');
         },
         error: function (err) {
            alert(JSON.stringify(err));
         }
      });
   });          
});
Posted in Uncategorized and tagged .

4 Comments

  1. I am getting error like this:

    {“error”:{“code”:”-2130242040, Microsoft.SharePoint.SPException”,”message”:{“lang”:”en-US”,”value”:”The e-mail message cannot be sent. Make sure the e-mail has a valid recipient.”}}}”

  2. Hi,

    Thanks for the info. But i want to send mail to multiple users. so how can i achieve that ?
    TIA.
    Reagrds,
    Sudheer

Leave a Reply

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