Powershell email alerts with html format

Skumar_CCSA
Skumar_CCSA used Ask the Experts™
on
hi,

i have script working.
When it runs it check for files and send email alert if not files and also if has files.
Currently it sending the email alerts text format.
Request experts help, want to send email with htm format.
and for errors it should bold the text and color RED.
can help...


#Requires -version 3.0
$WarningPreference="Continue"
function Send-Mail
{
  param (
    [string] $Email_Subject,
    [string] $Email_Body,
    [string] $Email_Attachment
  )
  Send-MailMessage -SmtpServer 'mailserver.com' `
  -Body $Email_Body `
  -From 'donotreply@mail.com' `
  -To 'admin@mail.com' `
  -Subject $Email_Subject `
  -Attachments $Email_Attachment
}
function Send-Error
{
  Write-Host "Sending Error Alert @ $emaildate"
  $subject = "ACTION REQUIRED : Error Alert @ $emaildate"
  $logcontents = get-content $Logfile | Out-String
  $Body = "Dear All,`n`nThis email sent by system for errors highlighted below."  + $logcontents + "`n Regards,`n Job Process"
  Send-Mail -Email_Subject $subject -Email_Body $Body -Email_Attachment $Logfile }
function send-status
{
  Write-Host "Sending Success Alert @ $emaildate"
  $subject = "INFO : Success Alert @ $emaildate"
  $logcontents = get-content $Logfile | Out-String
  $Body = "Dear All,`n`nThis email sent by system for your information"  + $logcontents + "`n Regards,`n Job Process"
  Send-Mail -Email_Subject $subject -Email_Body $Body -Email_Attachment $Logfile }

  #check if source files exists
$sourcefolder="D:\ACES\SOURCE"
$backupFolder="D:\ACES\Archive"

$files   = 'CAT.xml', 'BAT.xml', 'RAT.xml'
$success = $true
foreach( $file in $files )
{
       $filename = Join-Path $sourcefolder $file
       if( Test-Path -LiteralPath $filename )
       {
              LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) FILECHECK:`tFile exists: $filename"
       }
       else
       {
              $msj = "The source file '$fileName' doesn't exist"
              LogWrite "$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ERROR:`t$msj"
                    LogWrite -ForegroundColor Red $msj
              $success = $false
       }
}

if( -not $success  )
{
    LogWrite "`n$( Get-Date -Format "yyyy-MM-dd HH:mm:ss" ) ACTION REQUIRED:`tFiles missings($sourcefolder)"
    send-Error
    exit -1
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007

Commented:
Add the –BodyAsHtml parameter to your send-mailmessage cmdlet:

  Send-MailMessage -SmtpServer 'mailserver.com' `
  –BodyAsHtml `
  -Body $Email_Body `
  -From 'donotreply@mail.com' `
  -To 'admin@mail.com' `
  -Subject $Email_Subject `
  -Attachments $Email_Attachment

Open in new window



For errors, you can simply embed the tags in your body:

$Body = "Dear All,`n`nThis email sent by system for errors highlighted below.<b><font color='FF0000 '>"  + $logcontents + "</font></b>`n Regards,`n Job Process"
  Send-Mail -Email_Subject $subject -Email_Body $Body -Email_Attachment $Logfile 

Open in new window

Top Expert 2016

Commented:
This is a duplicate of an open question: https://www.experts-exchange.com/questions/29119166/Powershell-script-errors-logs-to-send-email.html in which the -bodyashtml was already mentioned.

Author

Commented:
firstly it is not duplicate..the previous question posted for email alerts and issues in receiving email alerts.
This question is more on HTML format and additionally the errors to be bold and colored.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial