Sharepoint - Send E-mail from javascript using REST API

In SharePoint 2013 On Premise we can send emails using REST API where we can utilise SP.Utilities.Utility.SendEmail for doing the job.

Note: The recipient is limited to a valid SharePoint user for security reasons.

function processSendEmails() {

    var from = '[email protected]',
        to = '[email protected]',
        body = 'Hello World Body',
        subject = 'Hello World Subject';

    // Call sendEmail function
    //
    sendEmail(from, to, body, subject);
}


function sendEmail(from, to, body, subject) {
    //Get the relative url of the site
    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": jQuery("#__REQUESTDIGEST").val()
        },
        success: function(data) {
            alert('Email Sent Successfully');
        },
        error: function(err) {
            alert('Error in sending Email: ' + JSON.stringify(err));
        }
    });
}

$(document).ready(function () {

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', processSendEmails);

});

Please refer this link for for detailed code, you shall realize the issue.

This link is also useful.

Also: Make sure that the recipient is limited to a valid SharePoint user for security reasons.

Info: You have to provide the sharepoint Id for the specific user in order to send this user an email!

The following is the code:

function sendEmail(from, to, body, subject) {
//Get the relative url of the site
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": jQuery("#__REQUESTDIGEST").val()
    },
    success: function(data) {
        alert('Email Sent Successfully');
    },
    error: function(err) {
        alert('Error in sending Email: ' + JSON.stringify(err));
    }
});
}

Please make sure that the email recipient is in the same domain as SP server.


You can use the function I have added and call it like

sendEmail("domain\\sender", "domain\\recipient","This is the body","Mail Subject");:


    var hostweburl;
    var appweburl;

    $(document).ready(function () {
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', sendEmail);
    });
    function sendEmail(from, to, body, subject) {    
    appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
    hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
    var urlTemplate = appweburl + "/_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) {
           console.log('success')
        },
        error: function (err) {
            console.log(JSON.stringify(err));
        }
    });
    }