combining PLAIN-TEXT and HTML table to send it in email

by thepathfinder at 2012-11-27 05:07:59

Hi, I have a multi-array ($ArrayTable) that I want to send to email for printing in HTML format. The solution below is working fine. I pass the $Result variable to a SMTP script to create and send the mail.

$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

$Result = $ArrayTable | Select-Object ID,NAME,STATUS | ConvertTo-HTML -head $a

BUT, I also want to add few extra plain-text that would be printed before the HTML table. So I am doing like this]

$MessageString = "This mail is about xyz. This mail is about xyz. This mail is about xyz. This mail is about xyz. This mail is about xyz. This mail is about xyz."

$Result = $ArrayTable | Select-Object ID,NAME,STATUS | ConvertTo-HTML -head $a
$Result = $MessageString + $Result

This is working, BUT I want to know if this is the right way to do it that combining a HTML table structure and a plain-text together, and put it in a variable $Result? If there is any chance this could fail? And, if there is any other efficient way to do this? Any help will be really appreciated.
by DonJ at 2012-11-27 07:25:41
No, it isn’t. You can’t really combine HTML and plain text, but you can certainly include a header like that in the HTML, above the table. It just has to go inside the <BODY> tag. Usually, you’d wrap the introductory text in <P></P> tags. The way you’re doing it is wrong, though, because you’re prepending text to an HTML page, which isn’t valid in a lot of browsers and renderers.

ConvertTo-HTML can do this correctly for you - just use the -PreContent parameter to put your "This mail is about" message. So, you’re already putting the style tag into the -Head parameter; you’d just additionally put "-PreContent $MessageString" on ConvertTo-HTML.

Have you seen my HTML Reporting free book at It comes with a module that does a lot of this for you, including styling, and includes a sample script of how to use the module.
by thepathfinder at 2012-11-27 09:02:32
Hi Don, I just tried adding -precontent $MessageString, but now there’s an error in email. In email, It’s showing "System.String" before the TABLE. BY the way, I downloaded the book, this looks very helpful for HTML reporting.
by DonJ at 2012-11-27 09:43:26
You may have constructed $MessageString as an array, which -PreContent won’t accept. Try

-PreContent ($MessageString | Out-String)

And see if that helps.
by thepathfinder at 2012-11-28 02:36:59
Hi Don, $MessageString is NOT an array, I just checked its a System.String.

I tried this, but haven’t helped:
$Result = $ArrayTable | Select-Object ID,NAME,STATUS | ConvertTo-HTML -head $a -PreContent ($MessageString | Out-String)

Here, $ArrayTable is an array but this shoudn’t be the problem. Do you think, since i’m combining an "ARRAY generated HTML table" with a "STRING $MessageString", causing an issue? Do I somehow need to convert HTML table also in string using Out-String ??
by DonJ at 2012-11-28 07:29:25
No, that isn’t the issue. -PreContent is specifically intended to do this. I can do:

Get-Service | Select Name,Status | ConvertTo-HTML -head ‘<title>whatever</title>’ -precontent ‘<h1>Heading</h1>’

And it works fine. You should be able to run that, too; if you can, but your original doesn’t work, then it’s something to do with the data you’re passing in $a or $messagestring. It isn’t $ArrayTable, I promise.