• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Scripts / Send Email using PowerShell

Send Email using PowerShell

03/01/2018 by Stephanos 10 Comments

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
}
Summary
Send Email using PowerShell
Article Name
Send Email using PowerShell
Description
Send Email using PowerShell. If you want to send email using PowerShell in HTML format, here is your answer. This can be used also in automated scripts to send the results to you. Stephanos Constantinou Blog - PowerShell Scripting
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Scripts Tagged With: Arithmetic Operators, Comparison Operators, ConvertTo-SecureString, Get-Content, Get-Date, Microsoft Office 365, New-Object, PowerShell If, PowerShell Operators, Send-MailMessage

Reader Interactions

Trackbacks

  1. Find users with password never expire using PowerShell - Stephanos Constantinou says:
    04/01/2018 at 15:04

    […] Send Email using PowerShell […]

    Reply
  2. PowerShell List all Users and Group Membership - Stephanos Constantinou says:
    06/01/2018 at 11:03

    […] Send Email using PowerShell […]

    Reply
  3. Set Active Directory user attributes automatically with PowerShell - Stephanos Constantinou says:
    09/01/2018 at 11:59

    […] Send Email using PowerShell […]

    Reply
  4. Email error variable in PowerShell - Stephanos Constantinou says:
    10/01/2018 at 11:59

    […] Send Email using PowerShell […]

    Reply
  5. Exchange custom attributes on groups automatically with PowerShell - Stephanos Constantinou says:
    25/01/2018 at 13:23

    […] Send Email using PowerShell […]

    Reply
  6. Remove Forwarding Office 365 PowerShell - Stephanos Constantinou says:
    25/01/2018 at 13:50

    […] Send Email using PowerShell […]

    Reply
  7. Enable Unified Messaging Office 365 using PowerShell. - Stephanos Constantinou says:
    31/01/2018 at 15:09

    […] Send Email using PowerShell […]

    Reply
  8. Office 365 PowerShell License Report - Stephanos Constantinou says:
    22/02/2018 at 10:16

    […] Send Email using PowerShell […]

    Reply
  9. PowerShell Get registry value data for backups - Stephanos Constantinou says:
    22/02/2018 at 10:21

    […] Send Email using PowerShell […]

    Reply
  10. Check if AD group is in correct OU - Stephanos Constantinou says:
    06/03/2018 at 11:14

    […] Send Email using PowerShell […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact