SharePoint Online: PowerShell to Send Email

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

PowerShell to Send Email in SharePoint Online:

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, lets use PowerShell’s native cmdlet Send-Mail message to send an Email from SharePoint online site.

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

#Config Parameters
$AdminSiteURL = "https://crescent-admin.sharepoint.com/"
$EmailFrom ="[email protected]"
$EmailTo ="[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 

If you want to add any attachment 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 email from the current site context.

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

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

This cmdlet also supports sending Email 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"

Salaudeen Rajack

Information Technology Professional with Two decades of SharePoint Experience.

2 thoughts on “SharePoint Online: PowerShell to Send Email

Leave a Reply