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.

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.
Loading...

Still running Exchange 2003? Time to get moving and start your upgrade. Find out how - Click Here