Automating Email Notifications using PnP PowerShell

Introduction

Sending emails programmatically is a powerful way to automate notifications in various scenarios. In this blog post, we will explore how to leverage the PnP PowerShell module to send emails seamlessly. Specifically, we will address the requirement of sending email notifications to site owners once a new SharePoint site is created.

What is PnP PowerShell?

PnP PowerShell is a popular PowerShell module that simplifies managing SharePoint and Office 365 environments, providing a seamless scripting experience for automating tasks and configurations.

Prerequisites

  • PnP PowerShell module installed
  • Permissions: Ensure that you have the necessary permissions to send emails from your SharePoint site.

Note. Below are the steps for sending email notifications using the PowerShell script.

Step 1. Connect to your SharePoint site

To begin, establish a connection to your SharePoint site using the Connect-PnPOnline cmdlet. Provide the URL of your site and your credentials when prompted.

Step 2. Define email properties

Next, define the properties of your email, such as the recipients, subject, and body. Use variables to store these values for easier customization.

Step 3. Sending the email

To send the email, use the Send-PnPMail cmdlet. Pass the recipient email addresses stored in an array to the -To parameter. You can also include CC recipients by passing their email addresses in another array to the -Cc parameter. Set the subject and body of the email using the respective variables.

Step 4. Disconnect from the SharePoint site

Once the email is sent, it's a good practice to disconnect from the SharePoint site using the Disconnect-PnPOnline cmdlet.

Complete script

#Global variables
$global:SiteTitle = "Practice demo site title"
$global:SiteCollUrl = "https://******.sharepoint.com/sites/PracticeSite-Santosh"
$global:SiteOwner1email = "gayatri@******.onmicrosoft.com"
$global:SiteOwner2email = "santosh@******.onmicrosoft.com"

#Step 1 Connect SharePoint site
Connect-PnPOnline -Url $global:SiteCollUrl -Interactive

Write-Host "Sending email to site owners..." -ForegroundColor Yellow

#Step 2 Define email properties
    $siteOwners = $global:SiteOwner1email, $global:SiteOwner2email
    $subject = "New Site Created: $($global:SiteTitle)"
    $body = @"
Dear Site Owner,

A new site has been created with the following details:

Site Name: $($global:SiteTitle)

Site URL: $($global:SiteCollUrl)

Thank you.

"@

# Replace line breaks with HTML <br> tags for email formatting
$body = $body -replace "`n", "<br>"

# Add spaces for indentation
$body = "`n`n`n" + $body + "`n`n`n"

#Step 3 Send the email with HTML formatting
Send-PnPMail -To $siteOwners -Subject $subject -Body $body
Write-Host "Email sent to site owners." -ForegroundColor Green

#Step 4 Disconnect SharePoint site
Disconnect-PnPOnline

Output

Email Notifications using PnP PowerShell

Conclusion

In this blog post, we've explored the basics of sending emails using PnP PowerShell. Sending emails using PnP PowerShell offers a convenient and efficient way to automate communication within your SharePoint environment. By following the steps discussed in this article, you can easily send emails to multiple recipients and even include CC recipients.          


Similar Articles