Send an HTML table in email via Powershell for Exchange 2007

I didn’t like sending a raw text table, so I put my data into an HTML table and it works great, but adds a lot of lines to your script.

No pain, no gain 🙂




untitled

# Get an email ready 
$smtpServer  = "smtp.foo.com"
$msg         = new-object Net.Mail.MailMessage
$smtp        = new-object Net.Mail.SmtpClient($smtpServer)
$msg.To.Add("admins@foo.com")
$msg.From    = "admins@foo.com"
$msg.Subject = "Hello world."
$msg.Body    = @() # Use the @ symbol for multi-line data

# Start off by creating the table and the header row
# Use the += operator to append
$html += "<table cellpadding='3'>
   <tr>
      <td>Owner</td>
      <td>Size</td>
      <td>Quota</td>
      <td>Percent</td>
      <td>Sent</td>
      <td>Calendar</td>
      <td>Contacts</td>
      <td>Deleted</td>
      <td>Drafts</td>
      <td>Notes</td>
      <td>Outbox</td>
      <td>Tasks</td>
   </tr>"

# This is where you'd loop through several data rows
If ($percent -gt .75) {
   $html += "<tr>"
   $html += "<td>" + $row.Owner + "</td>"
   $html += "<td>" + $row.Size + "</td>"
   $html += "<td>" + $row.Quota + "</td>"
   $html += "<td>" + $row.Percent + "</td>"
   $html += "<td>" + $row.Sent + "</td>"
   $html += "<td>" + $row.Calendar + "</td>"
   $html += "<td>" + $row.Contacts + "</td>"
   $html += "<td>" + $row.Deleted + "</td>"
   $html += "<td>" + $row.Drafts + "</td>"
   $html += "<td>" + $row.Notes + "</td>"
   $html += "<td>" + $row.Outbox + "</td>"
   $html += "<td>" + $row.Tasks + "</td>"
   $html += "</tr>"
}  # Done looping through everything
$html += "</table><br /><br />" # Close the table tag
 
# Send the file via email
$msg.IsBodyHTML = $true # Crucial for sending html mail
$msg.Body += "Here are the VIPs over 75% of their quota.<br /><br />" + $html
$smtp.Send($msg)


Leave a Reply

Your email address will not be published. Required fields are marked *