In order to send email from SharePoint Hosted App using REST, use the below code snippet.
var sendEmail = function (from, to, cc1, cc2, body, subject) {
var baseUrl = _spPageContextInfo.webServerRelativeUrl;
var urlTemplate = baseUrl + "/_api/SP.Utilities.Utility.SendEmail";
return getFormDigest().then(function (digest) {
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': [to, cc1, cc2] },
'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("An email was sent.");
},
error: function (args) {
console.log("We had a problem and an email was not sent.");
}
});
});
};
However there are some limitations:
1. The To user must be a valid SharePoint user, cannot be a external user. For security reasons, it doesn't allow sending email to external users.
2. The user who is logging and executing this code should have manage web permissions.
var sendEmail = function (from, to, cc1, cc2, body, subject) {
var baseUrl = _spPageContextInfo.webServerRelativeUrl;
var urlTemplate = baseUrl + "/_api/SP.Utilities.Utility.SendEmail";
return getFormDigest().then(function (digest) {
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': [to, cc1, cc2] },
'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("An email was sent.");
},
error: function (args) {
console.log("We had a problem and an email was not sent.");
}
});
});
};
However there are some limitations:
1. The To user must be a valid SharePoint user, cannot be a external user. For security reasons, it doesn't allow sending email to external users.
2. The user who is logging and executing this code should have manage web permissions.
Your website is really cool and this is a great inspiring article.
ReplyDeleteSharepoint helpdesk