SharePoint Online Automation - Email Notification Using PowerShell Script In SharePoint Online

In the following article, I have described how to check Site Quota in SharePoint Online Tenant using PowerShell.

Continuing with the Automation series, I am going to describe how to send email notifications after fetching the Quota Report and send them to the concerned team. As we know, sometimes, we need to send a confirmation email by email if the user is not in or working remotely. There is an easy PowerShell cmdlet called ‘Send-MailMessage’ that allows us to send an email to the Exchange Server. We can also use the same function in Office 365.

There are two major options in which we address the Office 365 mail infrastructure.

  • Anonymous SMTP session
  • Authenticated SMTP or TLS session

Here, we are going to use “Authenticated SMTP or TLS session”; in simple words, we are going to use user credentials and an encrypted communication channel.

We need to use a PowerShell script that needs to use the user credentials. Here, we can enter Username and password (using a variable we will store) or we can choose Read line method to ask for the Username and Password manually. Using Password in PowerShell is the easiest way to do it but it is something that is going to breach the security part. Your credentials will be in plain text and can be used by anyone if it is accessible for all users in your domain.

So, we can encrypt the password to avoid security breach using encrypted format. We will need to provide PowerShell the “user password” and the PowerShell command will take this password, encrypt the password, and save it in a text file. In supplementary words, the information is not readable by a human.

Below is the step by step process to “Send a mail PowerShell script” in Office 365.

  • Open SharePoint Management Shell with required permission and set Execution Policy as unrestricted or Remote signed. As by default, the PowerShell console doesn’t allow to run a PowerShell script.

Set-ExecutionPolicy

  • Once execution policy is set, we can use the below script in addition to the below “How to Get Quota” script.
    1. Import - Module Microsoft.Online.SharePoint.Powershell    
    2. $UserName = "Global account"    
    3. $Password = ‘Your Password '    
    4. #Write-host "Email has been sent and script has been executed successfully"  
    5. $Credentials1 = New-Object System.Management.Automation.PSCredential($UserName1,(ConvertTo-SecureString $Password1 -AsPlainText -Force))  
    6. Connect-SPOService -Url "https://spsolutiontips-admin.sharepoint.com/" -Credential $Credentials1  
    7. $SiteURL = "https://spsolutiontips.sharepoint.com/sites/psp/Lists/test"  
    8. $smtp = "smtp.office365.com"  
    9.  #$from = Read-host "Please provide your email address which you use for sending email"  
    10. $from = "admin@spsolutiontips.onmicrosoft.com"  
    11. $subject = "SharePoint Sites Quota Details"  
    12. $body = "Dear Team <br><br>"  
    13. $body += "Good day:) <br><br>"  
    14. $body += "Click <a href= $SiteURL > here </a> to check the uploaded SPO sites quota Details file <br><br>"  
    15. $body += "Thank you<br><br>"  
    16. $body += "SharePoint Support Team"  
    17. $body += "Please do not respond to this email"  
    18. #send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority low  
    19. Send-MailMessage -From $from -To "admin@spsolutiontips.onmicrosoft.com" -Subject $subject -Body $body -BodyAsHtml -smtpserver $smtp -Port 587 -Credential $Credentials1 -UseSsl  
    20. Write-host "Thank you, the script has been run successfully" -f Green  
  • In the above script, you need to modify this part.
    1. $UserName="Global account" Enter your Global Admin Account email address  
    2. $Password =Your Password' Enter Your Account Password, make sure you are keeping this password in the single quote or else it will throw an error.  
    3. $SiteURL = Your Library URL where Docs will save  
    4. $from = "You’re from Email address"  
  • As mentioned above, we are going to use a secure SMTP email connection by using Port 587.
  • Once all the modification is done, hit F5 on PowerShell ISE and wait for some time to get the result.

    PowerShell
  • Here, you can see your Output email has been sent to your defined email or distributed List.
  • Check your email box and verify the same.

    PowerShell