Ram Prasad
 

SharePoint REST API/Client Code to send mails with HTML/Rich Text Content to SharePoint Users

Jun, 16 2016
 
1 min/s
 
 

Following is the code snippet to send emails to local SharePoint users with HTML Content in the email body.

The recipients should be valid SharePoint Users. Mails cannot be sent to non-SharePoint users and external users.

function sendMail(toList, subject, mailContent) {
    var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail",
    restHeaders = {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "Content-Type": "application/json;odata=verbose"
    },
    mailObject = {
        'properties': {
            '__metadata': {
                'type': 'SP.Utilities.EmailProperties'
            },
            'To': {
                'results': toList
            },
            'Subject': subject,
            'Body': mailContent,
            "AdditionalHeaders":
                {
                    "__metadata":
                       { "type": "Collection(SP.KeyValue)" },
                    "results":
                    [
                        {
                            "__metadata": {
                                "type": 'SP.KeyValue'
                            },
                            "Key": "content-type",
                            "Value": 'text/html',
                            "ValueType": "Edm.String"
                        }
                    ]
                }
    }
};
return $.ajax({
    contentType: "application/json",
    url: restUrl,
    type: "POST",
    data: JSON.stringify(mailObject),
    headers: restHeaders
});

}

The highlighted headers in the mailObject above are required to specify the content type of the email as HTML. Without this header, the mail will be sent as a plain text and the HTML formatting is ignored.

Using this helper method

The above method can be used as a helper method to send mails from your client code or SharePoint Apps or Add-Ins. Make sure to include the required jQuery references

//SUBJECT OF THE EMAIL
var subject = "SUBJECT OF THE MAIL";
//CONTENT OF THE MAIL - CAN BE HTML OR PLAIN TEXT. 
var mailContent = "<h3>Some Heading for the mail</h3><p>Content</p><div>Content</div>";
//AN ARRAY HAVING THE RECIPIENT DETAILS.
//THE USERNAME/EMAIL-ID SHOULD BE OF A VALID SHAREPOINT USER. 
//EXTERNAL USERS OR EXTERNAL EMAIL IDS ARE NOT SUPPORTED
var toList = ["DOMAIN\\USERNAME", "USER1-EMAIL-ID@DOMAIN.COM"]

sendMail(toList, subject, mailContent).done(function (response) { console.log("Mail Sent."); }).fail(function () { console.error("Error while sending mail."); });