Sending emails in SharePoint is one of the most common automation tasks developers and power users handle. Whether it’s notifying users about a change, approval, or an update, automating this functionality boosts productivity and improves the user experience.
Let’s explore how to send email in SharePoint using the SharePoint REST API and PnPjs, discuss best practices, real-life use cases, cover what you need to know, and even compare it with alternative approaches like Power Automate and SP.Utilities.
โ What Is It?
In SharePoint Online, you can send emails programmatically using:
- The SharePoint REST API (
/_api/SP.Utilities.Utility.SendEmail
) - PnPjs, a lightweight JavaScript library that simplifies interactions with SharePoint REST APIs
These methods allow you to trigger emails from custom forms, web parts, or workflows without needing external mail servers or third-party connectors.
๐ก Why Do We Need It?
Sometimes SharePointโs out-of-the-box notifications arenโt flexible enough. For instance:
- You need to send a formatted HTML email on item creation.
- You want to CC or BCC users.
- You need to trigger emails based on custom logic from a form or app page.
This is where custom email solutions with REST API or PnPjs shine.
๐ฅ Who Uses It?
- SharePoint Developers creating custom apps or web parts
- Power Users integrating JavaScript into pages or modern forms
- IT Admins managing automation without relying entirely on Power Automate
- Consultants creating scalable notification features
๐ฆ Applications That Use It
- SharePoint Framework (SPFx) WebParts
- Script Editor / Modern Script WebParts with JavaScript
- Custom Forms in SharePoint Lists
- Power Apps with HTTP request connectors
- Flow/Power Automate (alternative option)
๐ ๏ธ Sample Implementations
๐น Option 1: Send Email Using SharePoint REST API
function sendEmail() {
var siteUrl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
contentType: 'application/json',
url: siteUrl + "/_api/SP.Utilities.Utility.SendEmail",
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'To': { 'results': ['user@domain.com'] },
'Subject': 'Custom Notification',
'Body': 'Hello! This is a custom email from SharePoint.'
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function () {
console.log('Email sent successfully!');
},
error: function (err) {
console.error('Error sending email: ', err);
}
});
}
๐น Option 2: Send Email Using PnPjs
import { sp } from "@pnp/sp/presets/all";
sp.utility.sendEmail({
To: ["user@domain.com"],
Subject: "PnPjs Email",
Body: "This email was sent using PnPjs!",
}).then(_ => {
console.log("Email Sent!");
}).catch(console.error);
โ Best Practices
- Always validate email addresses before sending
- Use HTML format carefully โ sanitize input to prevent injection
- Use try/catch or
.catch()
blocks for error handling - Avoid sending emails inside loops without batching
- Log success/failure for auditing
๐ง Things You Should Know
- Emails are sent from no-reply@sharepointonline.com
- You canโt change the sender without external SMTP services
- User permissions: Sender must have access to use utility service
- Rate limits apply โ donโt flood the system
- Not supported in SharePoint 2013 On-Prem without additional setup
๐ Alternatives & Comparisons
Feature | REST API | PnPjs | Power Automate | SP.Utilities in CSOM |
---|---|---|---|---|
Setup Complexity | Moderate | Easy (with SPFx) | Very Easy | Advanced (for legacy) |
UI Integration | Requires Custom Dev | Great for SPFx | Form-Based | Limited UI |
Error Handling | Manual | Promise-based | Built-in UI logging | Complex |
Authentication Handling | Manual Token/Digest | Handles with SPFx context | Handled by Flow | Needs legacy auth |
HTML Support | Yes | Yes | Yes | Yes |
Reusability | High | High | Moderate (per flow basis) | Low |
Best For | Custom Webparts, Forms | SPFx-based apps, modern forms | Business users, quick wins | On-prem scripting |
๐ Real-World Use Cases
- Notify a manager when a list item is updated
- Send onboarding emails from a custom HR form
- Alert security team on suspicious activity logs
- Share download links to external users securely
- Send confirmation emails on form submissions
โ๏ธ Pros and Cons
โ Pros
- Lightweight and flexible
- Great for custom UI experiences
- Integrates well with modern SharePoint development
โ Cons
- Limited customization of sender
- Requires permissions and tokens
- Not beginner-friendly for non-devs
๐ Useful References
- PnPjs Documentation โ sendEmail
- Microsoft Docs โ SendEmail REST API
- SPFx Setup and Usage of PnPjs
- REST API Rate Limits in SharePoint Online
Whether you’re working on a custom form, SPFx WebPart, or a user-driven portal, sending emails via SharePoint REST API or PnPjs gives you control, power, and flexibility. It’s ideal for developers who need more than just simple notifications and want to create a consistent and branded user experience.
If you’re new to PnPjs, Iโd recommend starting there โ it simplifies development and future-proofs your code. For power users and low-code enthusiasts, Power Automate might be a better fit for quick wins.