Send Email using PowerShell
Scenario:
Send Email using PowerShell.
I use a lot of scripts on a schedule basis either to perform changes in Active Directory, Microsoft Exchange OnPremise and Exchange Online. I had to get report on what changes have been done by the script, if there are any, and also to be informed in case of an error (there will be a separate post on error reporting emails). The below uses the Exchange Online to send the email to the intended recipients by login in to the server with a specific account. The email that is sent is in HTML format with High Importance. I personally divide the HTML and then I combine it again to send it and the only reason I am doing this is because I use it on my scripts and multiple values are generated. I fill with these values the cells of he table that I create. You will find the exact use of this on my other posts/scripts.
Related Links:
- PowerShell Scripts
- PowerShell Tutorials
- Encrypt Password with key using PowerShell
- Using the Get-Content Cmdlet – TechNet – Microsoft
- ConvertTo-SecureString – Microsoft Docs
- New-Object – Microsoft Docs
- Get-Date – Microsoft Docs
- Send-MailMessage – Microsoft Docs
Solution / Script:
$File = "C:\Scripts\Password.txt"
$Key = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32)
$EmailUser = "Script-User@domain.com"
$Password = Get-Content $File | ConvertTo-SecureString -Key $Key
$EmailCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $EmailUser,$Password
$To = 'User1@domain.com','User2@domain.com','User3@domain.com'
$From = 'Script-User@domain.com'
$EmailResult = ""
$EmailTemp = @"
<tr>
<td class="colorm"Value1</td>
<td>Cyprus</td>
</tr>
"@
$EmailResult = $EmailResult + "`r`n" + $EmailTemp
$EmailUp = @"
<style>
body { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; color:#434242;}
TABLE { font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TR {border-width: 1px;padding: 10px;border-style: solid;border-color: white; }
TD {font-family:Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif !important; border-width: 1px;padding: 10px;border-style: solid;border-color: white; background-color:#C3DDDB;}
.colorm {background-color:#58A09E; color:white;}
.colort{background-color:#58A09E; padding:20px; color:white; font-weight:bold;}
.colorn{background-color:transparent;}
</style>
<body>
<h3>Script has been completed successfully</h3>
<h4>Any text that you want to write.</h4> <table>
<tr>
<td class="colort">Column 1 Title</td>
<td class="colort">Column 2 Title</td>
</tr>
"@
$EmailDown = @"
</table>
</body>
"@
$Email = $EmailUp + $EmailResult + $EmailDown
if ($EmailResult -ne "") {
send-mailmessage `
-To $To `
-Subject "Subject of Email $(Get-Date -format dd/MM/yyyy)" `
-Body $Email `
-BodyAsHtml `
-Priority high `
-UseSsl `
-Port 587 `
-SmtpServer 'smtp.office365.com' `
-From $From `
-Credential $EmailCredentials
}


[…] Send Email using PowerShell […]