HTML output into Body of EMail

Welcome Forums General PowerShell Q&A HTML output into Body of EMail

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
5 days, 19 hours ago.

  • Author
    Posts
  • #128476

    Participant
    Points: 108
    Rank: Participant

    I have this script creating output in HTML as an attachment

    I would like to take the attachment and place it into the body of the email.

    I tried a few methods nothing see to pan out.

    Here is my current script

    $Server = hostname
    
    function Set-Email { $Params = @{
    
     Subject = "$Server Clients By IP Address"
    
     Body = "Clients By IP Address open attachment"
    
     Attach = "$Logfolder\$htmlfile"
    
     From = "no-reply@mynet.com"
    
     To = "systems-alert@mynet.com"
    
     smtpserver = "InternalRelay.MYNET.COM"
    
    }
    
    Send-MailMessage @Params
    
    }
    
    $LogFolder = "c:\util\logs"
    
    $LogFile = "clientIp.csv"
    
    $LogParser = "C:\Program Files (x86)\Log Parser 2.2"
    
    $htmlfile = 'ip.html'
    
    remove-item -path $LogFolder\$LogFile -Force
    
    remove-item -path $LogFolder\$htmlFile -Force
    
    $Query = @"
    
    SELECT Extract_Suffix(client-name,0,'=') as User,client-name as DN,client-software,
    
    client-software-version as Version,client-mode,client-ip,protocol
    
    from 'G:\Program Files\Microsoft\Exchange Server\V15\Logging\RPC Client Access\RCA*.log'
    
    WHERE (operation='Connect') GROUP BY User,DN,client-software,Version,client-mode,client-ip,protocol
    
    ORDER BY User" -i:CSV -nSkipLines:4 -o:CSV
    
    "@
    
    & $Logparser\LogParser.exe $Query | out-file $Logfolder\$LogFile
    
    $csv1 = import-csv -path $LogFolder\$logfile
    
    $reporthtml = $csv1 | ConvertTo-Html -Fragment
    
    $htmlhead="
    
    
    
    
    
    

    Report of Exchange

    " $htmltail = "" $htmlreport = $htmlhead + $reporthtml + $htmltail $htmlreport | Out-File $Logfolder\$htmlfile -Encoding UTF8 Set-Email

    This works using attachment

    I have tied this

    $bigbody = get-content("$LogFolder\$LogFile") | out-string

    added this also

    BodyAsHtml = $true

     

    Any ideas

     

    Thank you

     

    Tom

     

     

     

  • #128490

    Participant
    Points: 832
    Helping Hand
    Rank: Major Contributor

    Can't see where the $BigBody variable is used ? you can give $htmlreport directly to -Body with -BodyAsHtml switch.

  • #128493

    Participant
    Points: 108
    Rank: Participant

    KVPRASOON,,

    This was how I tried.

     

    $Server = hostname
    
    function Set-Email { $Params = @{
    
     Subject = "$Server Clients By IP Address"
    
     Body = "$Bigbody"
    
     BodyAsHtml = $true
    
     Attach = "$Logfolder\$htmlfile"
    
     From = "no-reply@tgcsnet.com"
    
     To = "systems-alert@tgcsnet.com"
    
     smtpserver = "InternalRelay.TGCSNET.COM"
    
    }
    
    Send-MailMessage @Params
    
    }
    
    $LogFolder = "c:\util\logs"
    
    $LogFile = "clientIp.csv"
    
    $LogParser = "C:\Program Files (x86)\Log Parser 2.2"
    
    $htmlfile = 'ip.html'
    
    remove-item -path $LogFolder\$LogFile -Force
    
    remove-item -path $LogFolder\$htmlFile -Force
    
    $Query = @"
    
    SELECT Extract_Suffix(client-name,0,'=') as User,client-name as DN,client-software,
    
    client-software-version as Version,client-mode,client-ip,protocol
    
    from 'G:\Program Files\Microsoft\Exchange Server\V15\Logging\RPC Client Access\RCA*.log'
    
    WHERE (operation='Connect') GROUP BY User,DN,client-software,Version,client-mode,client-ip,protocol
    
    ORDER BY User" -i:CSV -nSkipLines:4 -o:CSV
    
    "@
    
    & $Logparser\LogParser.exe $Query | out-file $Logfolder\$LogFile
    
    $csv1 = import-csv -path $LogFolder\$logfile
    
    $reporthtml = $csv1 | ConvertTo-Html -Fragment
    
    $htmlhead="
    
    
    
    
    
    

    Report of Exchange

    " $htmltail = "" $htmlreport = $htmlhead + $reporthtml + $htmltail $htmlreport | Out-File $Logfolder\$htmlfile -Encoding UTF8 $bigbody = get-content("$LogFolder\$LogFile") | out-string Set-Email

     

    I bolded the changes.

     

    You say "you can give $htmlreport directly to -Body with -BodyAsHtml switch."

    How do I do that??

    -BodyAsHTML  $htmlreport      ????

     

    Thanks

     

    Tom

     

     

     

     

  • #128499

    Participant
    Points: 108
    Rank: Participant

    I got it to work Thanks for leading me to the proper direction

     

     

You must be logged in to reply to this topic.