Conditional formatting an html email body

Hi,

I'm pulling some data out of a database and creating an email with an html body.  I'd like to highlight some of the rows in a colour if for example a particular column is flagged with a zero, I can call the column "expired".  I need it highlighted in html because the users who will be receiving the emails need it as easy as possible....

 I don't know a lot of HTML but do know a little SQL and Powershell.  Here is my code

  #Send email Function    

function sendMail($messagebody) {

     Write-Host "Sending Email"

     #SMTP server name
     $smtpServer = "xxxxx"

     #Creating a Mail object
     $msg = new-object Net.Mail.MailMessage

     #Creating SMTP server object
     $smtp = new-object Net.Mail.SmtpClient($smtpServer)

     #Email structure
     $msg.From = "sdfsdfs@sdfsdfs"
     $msg.ReplyTo = "dfgfdgdf@dfgdfgd"
     $msg.To.Add("dfgdfg@dfgdfgdf")

     $msg.subject = "**:-)**"
     $msg.IsBodyHtml = $true
     $msg.body = $messagebody

     #Sending email
     $smtp.Send($msg)

}


$TableHeader = "e Services"          ##The title of the HTML page
$OutputFile = "C:\MyReport.htm"         ##The file location

##set HTML formatting
$a = @"
<style>
BODY{background-color:white;}
TABLE{border-width: 1px;
  border-style: solid;
  border-color: black;
  border-collapse: collapse;
}
TH{border-width: 1px;
  padding: 0px;
  border-style: solid;
  border-color: black;
  background-color:#C0C0C0
}
TD{border-width: 1px;
  padding: 0px;
  border-style: solid;
  border-color: black;
  background-color:white
}
</style>
"@

$body = @"
<p style="font-size:25px;family:calibri;color:#ff9100">
$TableHeader
</p>
"@



$connString = "Data Source=Localhost\SQLEXPRESS,1433;Initial Catalog=Logons;User Id=Matrix; Password=Q@v652500;"

$QueryText = "exec dbo.LancasterWayBuildingRelated"

$SqlConnection = new-object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $connString
$SqlCommand = $SqlConnection.CreateCommand()
$SqlCommand.CommandText = "EXEC dbo.LancasterWayBuildingRelated"


$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $SqlCommand
$dataset = new-object System.Data.Dataset

$dataAdapter.Fill($dataSet)
$connection.Close()

##Return all of the rows and pipe it into the ConvertTo-HTML cmdlet, and then pipe that into our output file
$messagebody = $dataSet.Tables | Select-Object -Expand Rows |
ConvertTo-HTML -head $a body $body 
$messagebody | Out-File $OutputFile


#Write-Host $DataAdapter.Fill($dataset) ' records have been exported.'

#[string]$all = $dataset.tables[ 0 ] | Out-String

#Calling email function
if (!$messagebody) {} else
{sendMail $messagebody}




February 23rd, 2015 12:50pm

Here are a number of methods:

http://tech-comments.blogspot.com/2012/07/powershell-dynamically-color-posh.html

Free Windows Admin Tool Kit Click here and download it now
February 23rd, 2015 3:39pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics