Send email using rest api using SharePoint online/2013/2016

his SharePoint tutorial, we will discuss how to send email using the rest API in SharePoint Online or SharePoint 2013/2013. Here we will be using Rest API, jQuery, and SP.Utilities.Utility js to send email in SharePoint.

Send email using rest api SharePoint Online

Here in this example, I have created a sample contact us HTML form. Here there will be three fields and a button. On click of the button, it will send the email.

Here I have added the below script to a web part page in a script editor web part in SharePoint Online or SharePoint 2013/2016.

<h1>Contact Us</h1><br/>
Name: <br/>
<input type="text" name="myname" id="myname" placeholder="Enter your Name"/><br/>
Email: <br/>
<input type="text" name="myemail" id="myemail" placeholder="Enter your Email Address"/><br/>
Message: <br/>
<textarea rows="4" cols="50" id="mymessage"></textarea><br/><br/>
<button type="button" id="fbutton">Send Message</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#fbutton").click(function(){
var sitetemplateurl = _spPageContextInfo.webServerRelativeUrl + "_api/SP.Utilities.Utility.SendEmail";
var name = $("#myname").val();
var from = $("#myemail").val();
var msg = 'From: ' + name + '<br/><br/>' + 'Email: ' + from + '<br/><br/><br/>' + $("#mymessage").val();

$.ajax({
contentType: 'application/json',
url: sitetemplateurl,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': ['[email protected]'] },
'Body': msg,
'Subject':'New Email From SharePoint Online using RestAPI'
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert('Message sent successfully !!!');
$("#myname").val(");
$("#myemail").val(");
$("#mymessage").val(");
},
error: function (err) {
alert(JSON.stringify(err));
}
});
});
});
</script>

Once save the page, the page will appear like below:

send email using rest api sharepoint 2013
sharepoint rest api send email

After saving the page, after clicking on the button the email will go like below:

send email using rest api sharepoint online
sharepoint rest api send email

SharePoint rest api send email

The below code block helps to send email to a user with the help of jQuery and REST service in SharePoint 2013/2016/2019.

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>

<script>
function processSendEmails() {
    var from = ‘[email protected]’,
        to = ‘[email protected]’,
        body = ‘Hello World Body’,
        subject = ‘Hello World Subject’;
    alert(“Sending email to Krishna”);
    // Call sendEmail function
    sendEmail(from, to, body, subject);
    alert(“Email sent Successfully”);
}

function sendEmail(from, to, body, subject) {
    try {
        //Get the relative url of the site
        var siteurl = _spPageContextInfo.webAbsoluteUrl;
        alert(“Site URL=”+siteurl);
        var urlTemplate = siteurl + “/_api/SP.Utilities.Utility.SendEmail”;
        alert(“URLTemplate=”+urlTemplate);
        $.ajax({
            contentType: ‘application/json’,
            url: urlTemplate,
            type: “POST”,
            data: JSON.stringify({
                ‘properties’: {
                    ‘__metadata’: {
                        ‘type’: ‘SP.Utilities.EmailProperties’
                    },
                    ‘From’: ‘[email protected]’,
                    ‘To’: { ‘results’: [‘[email protected]’] },
                    ‘Body’: body,
                    ‘Subject’: subject
                }
            }),
            headers: {
                “Accept”: “application/json;odata=verbose”,
                “content-type”: “application/json;odata=verbose”,
                “X-RequestDigest”: jQuery(“#__REQUESTDIGEST”).val()
            },
            success: function(data) {
                alert(‘Email Sent Successfully’);
            },
            error: function(err) {
                alert(‘Error in sending Email: ‘ + JSON.stringify(err));
            debugger;
            }
        });
    }
    catch (error) {
    }
}
$(document).ready(function () {
    SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, processSendEmails);
});
</script>

How does this code works?

  • Save this script as sendemail.js
  • Upload the script in sharepoint document / asserts library
  • Create a web part page add the content editor web part
  • Add the javascript in a content editor web part or script editor web part

If we read the code closely this script gets triggered on page load.

The output will be as follows.

Send Email with jQuery and REST API in SharePoint
Send Email with jQuery and REST API in SharePoint

You may like following SharePoint Rest API tutorials:

This SharePoint tutorial we learned how to send email using Rest API in SharePoint. The same Rest API code will work in SharePoint 2013/2016 to send an email.

  • >