jack theripper
jack theripper

Reputation: 1

Convert data to table and send it as email in powershell

I have a CSV report which i want to send as email message in a pretty table.

My CSV file:

Blah1|blah2|blah3|blah4
Abc |def  |ghi  |ijk

My code

$file= import-csv c:\file.csv | convertto-html
Send-emailmessage -smtpserver “blah” -body $([string]$($file)) -subject “blah”

Upvotes: 0

Views: 1623

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 61103

As in my comment, the issue is probably because your table's border style is not visible which is usually set on your CSS.

A few points to note, there is no such cmdlet as Send-EmailMessage, you're probably referring to Send-MailMessage. In addition, you should use the -BodyAsHTML switch or the mail will be Plain Text (not sure if this is mandatory).

If you want to learn more about how to make your table prettier, I would recommend w3schools website which is very friendly.

Last point to note, if you're planning to use zebra striped tables, you should know that many mail clients are not compatible with nth-child(even) and as workaround, you should loop on even or odd rows to replace your <td> for a CSS class.

Give this a try:

$css = @'
<style>
* {
  font-family: Calibri, sans-serif;
  padding: 10px;
  margin: 0px;
  text-align: left;
  font-size: 20px;
}

table {
  border: 1px solid black;
  width: 100%;
  border-collapse: collapse;
}

td, th {
  border: 1px solid black;
}
</style>
'@

[string]$html = Import-Csv c:\file.csv | ConvertTo-Html -PreContent $css
Send-MailMessage -SMTPServer blah -Body $html -Subject blah -BodyAsHTML

Upvotes: 1

Related Questions