Format ConvertTo-HTML

Hello,

I am using PS to import a csv, convert it to HTML, then send it in an email. It all works but Gmail ignores tags, so I cannot use CSS, and I cannot figure out how to do inline styling in PS.

All I want to do at this point is provide some padding to make the HTML more readable.

Here is my code…

$smtpServer = "x.x.x.x"
$smtpFrom = "name@mail.org"
$smtpTo = "name@mail.org"
$messageSubject = "Blah Blah $((Get-Date).ToShortDateString())"

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

$summ = Import-Csv -Path 'C:\folder\file.csv' | ConvertTo-Html

$message.Body = 
@"
Hello,
Blah Blah.
$summ

Thank you,
Technology
"@

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

It basically comes out in the email in table format but not very readable. I would like to add some padding to the text and maybe even BOLD the column names. Everything I try just makes the table disappear from the email, as it is not being rendered past Gmails preprocessor.

I tried something like this…

$pad = $summ -replace "\",''

$summ = Import-Csv -Path 'C:\folder\file.csv' | ConvertTo-Html -$pad

…but I am not sure if this is properly done.

Advice much appreciated. Bear in mind I cannot link to a css file or use a tag.

Thank you.

I am trying to do exactly this

http://stackoverflow.com/questions/27805574/cell-spacing-and-convertto-html-fragment

But cannot get it to work.

Have you looked at our HTML Reporting free ebook (eBooks page)? It might not be exactly what you want, but you can certainly look at the technique and see if you can do something similar.

Thank you for the reply.

I did have a quick look and while it covers exporting to HTML using a lot of CSS, I cannot use CSS as this HTML is going straight into an email client that ignores style tags.

Looking at my code above, and comparing with the stackoverflow link in my reply, would you have any suggestions of how to write that?

You’ve got two basic methods. The first is to simply replace the generic tags with tags that have style applied. The second is dynamically creating the table by stepping through the data:

Awesome. Thank you for the reply!

I have amended my initial code and my script now looks like this…

$smtpServer = “x.x.x.x”
$smtpFrom = “RepairSummary@mail.org
$smtpTo = “creed.cordonier@mail.org
$messageSubject = “Weekly Student Device Repair Summary $((Get-Date).ToShortDateString())”

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

$summ = Import-Csv -Path ‘C:\folder\file.csv’

$message.Body = @"

Hello,
Here is your weekly repair summary.
    $($summ | ConvertTo-Html)
Thank you,
Technology

"@

$finalHTML = $message.Body -replace "<table", "<table border='0' cellspacing='0' cellpadding='10' style='border: 1px solid black; border-collapse: collapse;'"
$finalHTML = $finalHTML -replace "<th", "<th border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"
$finalHTML = $finalHTML -replace "<td", "<td border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"

#$finalHTML | Out-File "c:\folder\file.html"

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

If I un-comment $finalHTML | Out-File “c:\folder\filez.html” on line 28 - it spits out an html file that looks EXACTLY how I want the body of the email to look.

When I try to $smpt.Send($finalHTML), instead of just Out-File, I get an error:

'Cannot find an overload for “Send” and the argument count: “1”.

When I $smpt.Send($message), the body converts to HTML and sends the email without error, but none of the $finalHTML is applied.

I feel like I am close, and if I am thinking correctly, I should be able to $smpt.Send($finalHTML) the same way I can Out-file it and get the same result in email that I get in the out-file?

The example I gave was just showing you the results in a browser. Make sure you add the beginning tag (e.g. less than) in the replace like my example above to only replace the beginning tag in the html. Take a look at the code below and see if that works for you:

$summ = Import-Csv -Path 'C:\folder\file.csv'

$body = @"
    Hello,

    Here is your weekly repair summary.
    $($summ | ConvertTo-Html)

    Thank you,
    Technology
"@

$body = $body -replace "table", "table border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse;'"
$body += $body -replace "th", "th border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"
$body += $body -replace "td", "td border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"

$mailParams = @{
    To = "creed.cordonier@mail.org"
    From = "RepairSummary@mail.org"
    Subject = "Weekly Student Device Repair Summary $((GetDate).ToShortDateString())"
    Body = $body
    SmtpServer = "0.0.0.0"
    BodyAsHtml = $true
}

SendMailMessage @mailParams

thank you so much. I will look into your example and save it for the future.

In the end, I had to wrap $html.Body back into $finalHTML and then $smtp.Send($message). Here is that section of script…

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

$summ = Import-Csv -Path 'E:\folder\path.csv'

$message.body = @"

    
    Hello,
    Here is your weekly student device repair summary.
        $($summ | ConvertTo-Html)
    Thank you,
    Technology
    

"@

$finalHTML = $message.body -replace "<table", "<table border='0' cellspacing='0' cellpadding='10' style='border: 1px solid black; border-collapse: collapse;'"
$finalHTML = $finalHTML -replace "<th", "<th border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"
$finalHTML = $finalHTML -replace "<td", "<td border='0' cellspacing='0' cellpadding='0' style='border: 1px solid black; border-collapse: collapse; padding:5px;'"

$message.body = $finalHTML

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