# Format ConvertTo-HTML

**URL:** <https://forums.powershell.org/t/format-convertto-html/7165>\
**Category:** PowerShell Help\
**Created:** [September 8, 2016, 9:44am UTC](https://forums.powershell.org/t/format-convertto-html/7165 "2016-09-08T09:44:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 8, 2016, 9:44am UTC](https://forums.powershell.org/t/format-convertto-html/7165/1 "2016-09-08T09:44:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 8, 2016, 9:56am UTC](https://forums.powershell.org/t/format-convertto-html/7165/2 "2016-09-08T09:56:04Z")

</div>

I am trying to do exactly this

[http://stackoverflow.com/questions/27805574/cell-spacing-and-convertto-html-fragment](http://stackoverflow.com/questions/27805574/cell-spacing-and-convertto-html-fragment)

But cannot get it to work.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [September 8, 2016, 10:42am UTC](https://forums.powershell.org/t/format-convertto-html/7165/3 "2016-09-08T10:42:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 8, 2016, 10:50am UTC](https://forums.powershell.org/t/format-convertto-html/7165/4 "2016-09-08T10:50:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 8, 2016, 11:34am UTC](https://forums.powershell.org/t/format-convertto-html/7165/5 "2016-09-08T11:34:27Z")

</div>

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:

> <https://gist.github.com/rasimmers/81a2767e70f0d05066345f15b96c5063>

---

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 8, 2016, 12:43pm UTC](https://forums.powershell.org/t/format-convertto-html/7165/6 "2016-09-08T12:43:24Z")

</div>

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](mailto:RepairSummary@mail.org)”  
$smtpTo = “[creed.cordonier@mail.org](mailto: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)

---

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 8, 2016, 12:47pm UTC](https://forums.powershell.org/t/format-convertto-html/7165/7 "2016-09-08T12:47:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [September 8, 2016, 2:56pm UTC](https://forums.powershell.org/t/format-convertto-html/7165/8 "2016-09-08T14:56:19Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![creed-cordonier](https://avatars.discourse-cdn.com/v4/letter/c/c68b51/32.png) [@creed-cordonier](https://forums.powershell.org/u/creed-cordonier)\
**Post date:** [September 9, 2016, 7:30am UTC](https://forums.powershell.org/t/format-convertto-html/7165/9 "2016-09-09T07:30:26Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/format-convertto-html/7165/10 "2024-05-16T20:40:21Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


