How to Send SMTP Email Using PowerShell (Part 4)

In the last part of this article series on sending email using PowerShell I demonstrated how to create HTML emails from PowerShell scripts.

One of the downsides to creating HTML output from PowerShell scripts is that the formatting is not very nice. The default formatting results in messages that look something like this.

The HTML for that email message was created by piping the output from one command into the ConvertTo-HTML cmdlet. Fortunately ConvertTo-HTML has the -Head parameter that can be used to add style information to the HTML that it generates.

For example, by adding this line to the script used in the last article we can change the font to something a little nicer to look at.

$style = "< style>BODY{font:arial 10pt;}< /style>"

Note: extra spaces have been added to the style tags so that WordPress does not remove them.

Then modify the ConvertTo-HTML cmdlet to add the -Head parameter.

$message.Body = Get-ExchangeServer | Select-Object Name,ServerRole | ConvertTo-Html -Head $style

This time when the script is run the email looks like this instead.

That is a little better, but we can do a lot more. How about we add some more styles to the table and really make it look nice.

$style = "< style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "< /style>"

Again note that extra spaces were added to the style tags above to preserve them in WordPress.

Now the script produces an email that looks like this.

Although this requires you to know a little bit about how HTML styles work, it is a pretty easy topic to learn and once you get a few working styles in your scripting tool kit you can just use them over and over again.

About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Connect with Paul on Twitter and Google+.

Comments

  1. Very useful tip!
    Thanks a lot.

  2. Tim Preston says:

    Hi Paul, great write up.

    I have followed all 4 parts of your series and have had good success. The only thing I can’t get working is the table formatting from this latest part. The tags don’t seem to be getting processed, the HTML code is simply printed at the top of email body verbatim (before the cmdlet’s output).

    Here’s my script:

    $smtpServer = “localhost”
    $smtpFrom = “name1@test.com”
    $smtpTo = “name2@test.com”
    $messageSubject = “Test”

    $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
    $message.Subject = $messageSubject
    $message.IsBodyHTML = $true

    $style = “BODY{font-family: Arial; font-size: 10pt;}”
    $style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
    $style = $style + “TH{border: 1px solid black; background: #dddddd; padding: 5px; }”
    $style = $style + “TD{border: 1px solid black; padding: 5px; }”
    $style = $style + “”

    $message.Body = Get-MailboxExportRequest | Get-MailboxExportRequestStatistics | Select-Object SourceAlias,Status | ConvertTo-Html -Head $style

    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $credentials=new-object system.net.networkcredential(”user”,”password”)
    $smtp.credentials=$credentials.getcredential($smtpServer,25,”basic”)
    $smtp.Send($message)

    Thanks!

  3. Tim Preston says:

    Oops, sorry – my copy/paste didn’t work so well there. The ‘$style’ code should be:

    $style = “BODY{font-family: Arial; font-size: 10pt;}”
    $style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
    $style = $style + “TH{border: 1px solid black; background: #dddddd; padding: 5px; }”
    $style = $style + “TD{border: 1px solid black; padding: 5px; }”
    $style = $style + “”

  4. Hey thanks for this. If you wanted to add more than one table in the body of an email, how would you do so using this script?

    Thanks

    • Matt Pollock says:

      Hello Paul,

      Great script, it works very well.
      Just one question though – how do you specify multiple email recipients?

      Thanks

      • As a comma-separated list.

        Eg, “address1@domain.com”,”address2@domain.com”

        • Matt Pollock says:

          Thanks for the reply Paul.
          I tried a comma separated list before posting the comment – the error returned is below:

          New-Object : Exception calling “.ctor” with “2″ argument(s): “The specified string is not in the form required for an e-mail address.”

          The script runs but the only the first email address in the list receives the email.

          Script is below:

          $smtpServer = “mail.domain.localnet”
          $smtpFrom = “ExchangeReports@domain.com”
          $smtpTo = “me@domain.com”,”me1@domain.com”
          $messageSubject = “MailBox Report for $((Get-Date).ToShortDateString())”

          $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
          $message.Subject = $messageSubject
          $message.IsBodyHTML = $true

          $style = “BODY{font-family: Arial; font-size: 10pt;}”
          $style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
          $style = $style + “TH{border: 1px solid black; background: #7FFF00; padding: 5px;}”
          $style = $style + “TD{border: 1px solid black; padding: 5px;}”
          $style = $style + “”

          $message.Body = Get-MailboxDatabase `
          | Get-MailboxStatistics | ?{$_.IsArchiveMailbox -eq $false} `
          | Sort-Object TotalItemSize -Desc `
          | Select-Object DisplayName, Database, TotalItemSize, LastLogonTime, LastLoggedOnUserAccount -First 20 `
          | ConvertTo-Html -Head $style

          $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
          $smtp.Send($message)

        • Ah, I really should update this article series to get rid of the PowerShell 1.0 references.

          So if you look back at this part you’ll see the PowerShell 2.0 method for sending email, which uses a proper cmdlet Send-MailMessage.

          http://exchangeserverpro.com/powershell-how-to-send-email/

          An example:

          ==================================
          $smtpsettings = @{
          To = “administrator@exchangeserverpro.net”
          From = “exchangeserver@exchangeserverpro.net”
          Subject = “Exchange CAS Health Report – $now”
          SmtpServer = “smtp.exchangeserverpro.net”
          }

          Send-MailMessage @smtpsettings -Body $whatever
          ==================================

          So in this case that To should accept multiple comma-separated addresses.

          If you’re looking for some more comprehensive examples most if not all of the scripts available in http://inside.exchangeserverpro.com have code for sending email.

  5. Matt Pollock says:

    Ah right I see, thanks Paul.
    I’ve amended the script, which now works perfectly.
    Thanks again.
    Matt

    $smtpsettings = @{
    To = “me@domain.com”,”me1@domain.com”
    From = “ExchangeReports@domain.com”
    Subject = “MailBox Report for $((Get-Date).ToShortDateString())”
    SmtpServer = “mail.domain.localnet”
    }

    $style = “BODY{font-family: Arial; font-size: 10pt;}”
    $style = $style + “TABLE{border: 1px solid black; border-collapse: collapse;}”
    $style = $style + “TH{border: 1px solid black; background: #7FFF00; padding: 5px;}”
    $style = $style + “TD{border: 1px solid black; padding: 5px;}”
    $style = $style + “”

    $MailBoxReport = Get-MailboxDatabase `
    | Get-MailboxStatistics | ?{$_.IsArchiveMailbox -eq $false} `
    | Sort-Object TotalItemSize -Desc `
    | Select-Object DisplayName, Database, TotalItemSize, LastLogonTime, LastLoggedOnUserAccount -First 20 `
    | ConvertTo-Html -Head $style

    Send-MailMessage @smtpsettings -Body $MailBoxReport -BodyAsHTML

  6. Hi Paul,

    I am new to Powershell and I am trying to make use of your code above. Basically I want to change font size half way through an email but so far I have failed. The email is set as HTML, and I can get the text to change to italic, but I am stumped on the font size change.

    Where the font changes to italic below, I want that font to change to Arial, font size 8. – Many Thanks – Larry

    #Message Body
    $msg.IsBodyHTML = $True
    $msg.Body = ”Please See Attached Files
    Many Thanks,
    Larry (Test) xxxx

  7. vijender Reddy says:

    Hi All,

    I need a pwershell script to get the email alert when site went to disable state in IIS 7.0

    can some one help on this, Thanks

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.