To send emails from SharePoint, we use Power Automate / SharePoint Utility Email using C#.
What if you want to send emails from your client-side code. Yes, it's possible by using /_api/SP.Utilities.Utility.SendEmail
Note
Your recipient must be a SharePoint site user. You can't send emails from SharePoint to other emails that are not in your organization.
In this blog, you will learn how to send emails using JavaScript, with a simple project, "Leave Request Application"
Let's get started...
Prerequisites
- SharePoint List with columns,
- List name - LeaveTracker
- Columns - Title, ManagerEmail, EmployeeID, LeaveStartDate, LeaveEndDate, LeaveType, Comments
- Site Page - https://{yourtenantname}.sharepoint.com/sites/DemoSite/SitePages/LeaveApplication.aspx
- Add your code files to Site Assets, you can get the code files from my GitHub.
Screenshot of our application,

Step 1
Refer the JS and CSS files,
- <link type="text/css" rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
- <script type="text/javascript" src="../SiteAssets/SendEmail.js"></script>
Step 2
Get the field values from HTML, on button click, "Send Email"
- var EmpName, ManagerEmail, StartDate, EndDate, EmpID, Comments, LeaveType = '';
- function getDetails() {
- EmpName = $("#Emp_Name").val();
- ManagerEmail = $("#ManagerEmail").val();
- StartDate = document.getElementsByName("StartDate")[0].value;
- EndDate = document.getElementsByName("EndDate")[0].value;
- EmpID = document.getElementsByName("Emp_ID")[0].value;
- Comments = document.getElementsByName("Comments")[0].value;
- LeaveType = $("input[type='radio'][name='LeaveType']:checked").val();
- fnCreateItem(); //REST API POST Method
- processSendEmails(); //Sends Email
- }
Step 3
Now store the values to your SharePoint list, using REST API
Note
SharePoint List is not mandatory, you can send an email directly.
- function fnCreateItem() {
- var requestUriPOST = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('LeaveTracker')/items?";
- $.ajax({
- url: requestUriPOST,
- type: "POST",
- data: JSON.stringify({
- __metadata: {
- type: "SP.Data.LeaveTrackerListItem"
- },
- Title: EmpName,
- ManagerEmail: ManagerEmail,
- EmployeeID: EmpID,
- LeaveStartDate: StartDate,
- LeaveEndDate: EndDate,
- LeaveType: LeaveType,
- Comments: Comments
- }),
- headers: {
- "accept": "application/json;odata=verbose",
- "content-Type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val(),
- "X-HTTP-Method": "POST"
- },
- success: function(data) {
- console.log("Success");
- },
- error: function(error) {
- console.log(error.responseText);
- }
- });
- }




Venkata RamanaPosted May 3, 2024, 5:59 AM
In Your code From address is Eg '[email protected]', but email template shows Demosite <:no- [email protected]>.. Is code takes site name by default?