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 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.

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 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

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!

4 thoughts on “SharePoint Online: PowerShell to Send Email

Leave a Reply

Your email address will not be published.