How to send email using PowerShell in Office 365

In this Office 365 automation article, we will discuss how to send an EMail notification using PowerShell script in Office 365, after fetching Quota Report and send it to the concerned team and Distributed List.

As we know, sometimes we need to send a confirmation email by email if a user is not in or working remotely. There is an easy PowerShell cmdlet called ‘Send-MailMessage’ that allows us to send an email with Exchange Server. The same function we can also use 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 user credentials and an encrypted communication channel.

We need to use a PowerShell script that needs to use user credentials, here we can enter User Name and password (using Variable we will store) or we can choose the Read line method to ask User Name and Password manually.

Using Password in PowerShell is the easiest way to do but its something 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 an 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 called to encrypt the password.

Below is the step by step command 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 us to run a PowerShell script.

Set-ExecutionPolicy

Once the execution policy set we can use the below script in addition to below “How to Get Quota” script.

Import - Module Microsoft.Online.SharePoint.Powershell
$UserName = "Global account"
$Password = ‘Your Password '
#Write-host "Email has been sent and script has been executed successfully"
$Credentials1 = New-Object System.Management.Automation.PSCredential($UserName1,(ConvertTo-SecureString $Password1 -AsPlainText -Force))
Connect-SPOService -Url "https://spsolutiontips-admin.sharepoint.com/" -Credential $Credentials1
$SiteURL = "https://spsolutiontips.sharepoint.com/sites/psp/Lists/test"
$smtp = "smtp.office365.com"
#$from = Read-host "Please provide your email address which you use for sending email"
$from = "admin@spsolutiontips.onmicrosoft.com"
$subject = "SharePoint Sites Quota Details"
$body = "Dear Team <br><br>"
$body += "Good day:) <br><br>"
$body += "Click <a href= $SiteURL > here </a> to check the uploaded SPO sites quota Details file <br><br>"
$body += "Thank you<br><br>"
$body += "SharePoint Support Team"
$body += "Please do not respond to this email"
#send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority low
Send-MailMessage -From $from -To "admin@spsolutiontips.onmicrosoft.com" -Subject $subject -Body $body -BodyAsHtml -smtpserver $smtp -Port 587 -Credential $Credentials1 -UseSsl
Write-host "Thank you, the script has been run successfully" -f Green

In the above script, you need to modify the below part.

  • $UserName=”Global account” Enter your Global Admin Account email address
  • $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
  • $SiteURL = Your Library URL where Docs will save
  • $from = “You’re from Email address”

As mentioned in the above, we are going to use secure SMTP email connections by using Port 587.

Once all modification is done, hit F5 on PowerShell ISE and wait for some time to get the result.

send email powershell office 365
send email powershell office 365

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

send email from office 365 using powershell
send email from office 365 using powershell

Read some SharePoint PowerShell tutorials:

This PowerShell tutorial, we learned how to send email notification using PowerShell in Office 365.

  • >