This post was most recently updated on July 31st, 2024
In SharePoint Online we have used the Workflow to send an email notification to the user/group, but in some cases, we don’t want to create a workflow just for email.
For more info about the email trigger using workflow click HERE.
In this article, I will share how can we send emails using REST API (SP.Utilities.Utility.SendEmail).
Here are the keynotes and observations of send email method
- The recipients should be valid SharePoint Users. Mails cannot be sent to non-SharePoint users and external users. Any email accounts that have not been authenticated to SharePoint will throw an error.
- We cannot add an attachment with this API call, but we can add an inline attachment, I have tested with the image which is hosted on same email SharePoint domain. External domain image can be work if we change image to base64 and attach that.
- The SP.Utilities.Utility.SendEmail does not support to send emails to the Distribution group/list users.
- There is a limitation of the max size of email content.
- The format of your “To” variable must be an Array which contains emails (or domain\login) like var recipients = [“a@b.com”,”b@b.com”];
- It uses SP.Utilities.Utility.SendEmail
- You can use HTML tag [a,p,div etc.] in the email body, in case it does not work add Additional Headers
- Make sure to include the required jQuery references
- By default SharePoint use Site Title as an email header, to change the “From” headers in email, navigate to the Site Settings -> Look and Feel -> Look and Feel -> Title, description, and logo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
var adminDashboardURL='', MailBody = '', MailSubject = '',addedUserName="Demo User"; var adminDashboardURL = _spPageContextInfo.webAbsoluteUrl + "/Pages/DemoPage.aspx"; var receiversObj ='youremailid@example.com'; MailBody = '<p>Hello Admin,<p> <p>New tasks have been submitted. Please see additional details in the <a href="' + adminDashboardURL + '" target="_blank">admin dashboard</a>.</p><p>Thanks</p>'; MailSubject = 'New tasks have been submitted by ' + addedUserName; var taMailBody = { properties: { __metadata: { 'type': 'SP.Utilities.EmailProperties' }, From: "From: no-reply@sharepointonline.com", To: { 'results': [receiversObj] }, Body: MailBody, Subject: MailSubject, } }; $.ajax({ contentType: 'application/json', url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail", type: "POST", data: JSON.stringify(taMailBody), headers: { "Accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val() }, success: function (data) { console.log("Success"); }, error: function (data) { console.log("Error: " + JSON.stringify(data)); } }); |
Additional headers for the email
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
"AdditionalHeaders": { "__metadata": { "type": "Collection(SP.KeyValue)" }, "results": [ { "__metadata": { "type": 'SP.KeyValue' }, "Key": "content-type", "Value": 'text/html', "ValueType": "Edm.String" } ] } |
Here is the output:
Hope you find it helpful. If you liked this article, then please share and comment. Your comment and suggestion will be greatly appreciated.
Dear Chintan Upadhayay,
I know its been almost a year, but in the article you mention that we can add inline attachments to the email. Can you explain it in more details please? I try to send and email with inline images, but not sure how to do it.
Thanks in advance,
Daniel
Hope this article help you
https://stackoverflow.com/questions/27377376/how-do-i-send-email-with-inline-attachments