Thursday, October 15, 2015

Send email using SP.Utilities and Javascript

Use this below function to send an email to set people using JavaScript.

var toArr = new Array();
var ccArr = new Array();

function sendEmail() {
    debugger;
    var urlTemplate = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";
    var formDigest = document.getElementById("__REQUESTDIGEST").value;
    jQuery.ajax({
        contentType: 'application/json',
        url: urlTemplate,
        type: 'POST',
        data: JSON.stringify({
            'properties': {
                '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
                'From': from,
                'To': { 'results': toArr },
                'CC': { 'results': ccArr },
                'Subject': subject,
                'Body': body
            }
        }),
        headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": formDigest
        },
        success: function (data) {
           
           //Success info..
        },
        error: function (err) {        
            console.log("sendEmail function : " + err);
        }
    });
}

No comments:

Post a Comment