SharePoint Online: PowerShell to Send Email

Did you know that you can use PowerShell to send emails from SharePoint Online? This is a great way to automate email-based workflows, such as sending notifications or alerts. In this blog post, we’ll show you how to do this using PowerShell.

We can send emails from SharePoint Online, either with SPUtility’s SendEmail function or with the PowerShell cmdlet Send-MailMessage. Here are examples of both cases:

PowerShell to Send Email in SharePoint Online:

This blog post will show you how to use PowerShell to send an email from SharePoint Online. We will walk through example scripts that will send an email using the native SharePoint and PowerShell ways!

Here is how to send an E-mail from SharePoint Online using PowerShell Client-Side Object Model (CSOM) script:

Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters                                                                 
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$EmailFrom ="[email protected]"
$EmailTo = "[email protected]"
$Subject ="SharePoint Online Storage Report"

#Setup Credentials and connect
$Cred = Get-Credential
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get Storage Usage of All Site collections
$SiteStorage = Get-SPOSite -Detailed | Select Url, StorageUsageCurrent
       
#Setup the site context for SPUtility SendEmail
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup Email
$EmailProperties = New-Object Microsoft.SharePoint.Client.Utilities.EmailProperties
$EmailProperties.From = $EmailTo
$EmailProperties.To = [String[]] $EmailTo
$EmailProperties.Subject = $Subject
$EmailProperties.Body = $SiteStorage | Convertto-html
[Microsoft.SharePoint.Client.Utilities.Utility]::SendEmail($Ctx,$EmailProperties)
$Ctx.ExecuteQuery()

This sends Email using SPUtility’s SendMail method, with storage consumption of all site collections in SharePoint Online. Here, From and To emails are Email addresses within the organization.

SharePoint Online PowerShell to Send Email

SharePoint Online PowerShell to Send Email:

This time, let’s use PowerShell’s native cmdlet Send-MailMessage to send an Email from a SharePoint Online site. Sending emails in Sharepoint Online using PowerShell is a quick and easy way to communicate with colleagues and collaborators. To get started, open the PowerShell console and type the following script:

Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Parameters
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$EmailFrom ="[email protected]"
$EmailTo [email protected]("[email protected]","[email protected]")
$EmailSubject ="Site Collection Storage Utilization Report"
$SMTP ="smtp.office365.com"

#Setup Credentials and connect
$Cred = Get-Credential
Connect-SPOService -Url $AdminSiteURL -Credential $Cred

#Get Storage Usage of All Site collections
$SiteStorage = Get-SPOSite -Detailed | Select Url, StorageUsageCurrent | Convertto-html | Out-String

#Send Email
Send-MailMessage -from $EmailFrom -To $EmailTo -Subject $EmailSubject -Body $SiteStorage -BodyAsHtml -smtpserver $SMTP -usessl -Credential $Cred -Port 587 

The above example shows how to send an email to multiple recipients, and set the subject and body of the email. You will need to specify your own values for the $AdminSiteURL, $EmailFrom, $EmailTo, and $EmailSubject variables. If you want to add any attachments to the E-mail, use:

#send-mailmessage in powershell with multiple attachments
$Attachments = "D:\Reports\SiteDeleteRpt1.csv","D:\Reports\SiteDeleteRpt2.csv"
  
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -Body $EmailBody -BodyAsHTML -Attachments $Attachments -SmtpServer $SmtpServer -Credential $Cred -Port 587

SharePoint Online: PnP PowerShell to send email

We can also send E-mail from PowerShell in SharePoint Online using Send-PnPMail cmdlet. This sends an email from the current site context.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#sharepoint online powershell send email
Send-PnPMail -To [email protected] -Subject "Welcome" -Body "Welcome to Crescent Inc."

This cmdlet also supports sending emails from a user of the tenant.

Send-PnPMail -To [email protected] -Subject "Welcome" -Body "Welcome to Crescent Inc." -From [email protected] -Password "Password of the From User"

Import from CSV File – Format to HTML Table – and Send Email using PowerShell

In another requirement, I had to read the data from a CSV file, format it to an HTML table and then send it through email. Here is the PowerShell script to do that:

#Parameters
$CSVPath  = "C:\Temp\ADGroups.csv"
$EmailTo = "[email protected]"

#Import Data from CSV File
$CSVFile = Import-Csv $CSVPath

#Define CSS Styles
$HeadTag = @"
<style type="text/css">
table {
 font-size:11px; color:#333333; border-width: 1px; border-color: #a9c6c9;
 border: b1a0c7 0.5pt solid; border-spacing: 1px; border-collapse: separate;   
}
 
th {
border-width: 1px; padding: 5px; background-color:#8064a2; font-size: 14pt; font-weight: 600;
border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: white;
}
 
td {
 border: #b1a0c7 0.5pt solid; font-family: Calibri; height: 15pt; color: black; 
 font-size: 11pt; font-weight: 400; text-decoration: none; padding:5px;  
}
 
tr:nth-child(even) { background-color: #e4dfec; }
</style>
"@

#Frame Email Body
$EmailBody = $CSVFile | ConvertTo-Html -Head $HeadTag -PreContent "<h1>Site Users and Groups - Report</h1>" | Out-String

#Send Email
Send-PnPMail -To $EmailTo -Subject "Users and Groups Report" -Body $EmailBody

Here is the Email in action:

powershell sharepoint online send email

PowerShell can help you send bulk emails quickly and easily, saving you time and hassle.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

5 thoughts on “SharePoint Online: PowerShell to Send Email

Leave a Reply

Your email address will not be published. Required fields are marked *