In this article i will explain you how to send E-Mails through REST API in SharePoint.
We can send E-Mails through REST API using SP.Utilities.Utility.SendEmail.
Here is code to send E-Mail using REST API in SharePoint
$.ajax({ contentType: 'application/json', url: urlEmail, type: "POST", data: JSON.stringify({ 'properties': { '__metadata': { 'type': 'SP.Utilities.EmailProperties' }, 'Body': 'Hello user.. You got mail from codeplayandlearn.wordpress.com', 'To' : { 'results': ['admin@codeplayandlearn.com'] }, 'Subject': "E-Mail From REST API"; } }), headers: { "Accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: function (data) { alert("Email Send Successful."); }, error: function (err) { alert(err.responseText); } });
Here you don’t need to specify from E-Mail Id.
And make sure that To(Send to) E-Mail id should be E-Mail Id of any user from SharePoint Users.
And also make sure that metadata of Email should be
'type':'SP.Utilities.EmailProperties'
If this method not work or still you are getting error while sending E-Mails then try this method
Using separate function to send E-Mail from REST API
function processSendEmails() { var from = 'system@codeplayandlearn.com', to = 'member@codeplayandlearn.com', body = 'Dear member you got mail from Code Play and Learn', subject = 'Email from REST API'; // 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); });
Here i have created separate function to send E-Mail in which i am passing information of E-Mails.
By using this methods you can send E-Mails through REST API in SharePoint.
If this blog helps you in understanding SharePoint please share and like this blog.
Happy coding..!!
Learn code while playing with it..!!
Advertisements
Thank you.
FYI, I can only call your function with the below info:
sendEmail(“i:0#.w|domain\\login”, “i:0#.w|domain\\login”, “Your body message”, “Your subject message”);
LikeLike
how to send email to multiple users?
LikeLike
External users cannot be the recipients. It only works with sharepoint internal users. Also sending the email like abc@organization.org seems to be failing. Only sending the email through claim|domain\\userid seems to be working.
LikeLike