Send e-mail message formated as html with css

I´ve modified a script that is searching for today’s file.
If the file of today’s exist an e-mail is sent.
The problem is when I´m trying to send the e-mail in html format with CSS.
The e-mail content is only publish html-code like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
body, h1 {
        font-family:Arial, Helvetica, sans-serif
}
</style>
<title>Report</title>
</head>

<body>
<h1>Daily Report</h1>
<table width="300" border="1">
  <tr>
    <td>Files found from today</td>
    <td>0</td>
  </tr>
  <tr>
    <td>Old files found</td>
    <td>0</td>
  </tr>
</table>

</body>
</html>

Here´s the powershell code, maybe a little clumsy so far until I made it work:
(The script is searching for today´s file in a directory and sending a e-mail report to recipient)

$Directories = "E:\Source" 
$DestDirectory = "E:\Destination"   
$ThisDay = [datetime]::Today
$OtherFiles = 0
$TodaysFiles = 0
 Get-ChildItem $directories |
     ForEach-Object{
         if ($_.Length -eq 0 -and $_.CreationTime.Date -ne $ThisDay){  # What about non-zero length files?
             $OtherFiles++
         Copy-Item "E:\Soruce\*.txt" -Destination "E:\Destination"
         }
         #Kopierar dagens filer till Destination
         elseif($_.Length -eq 0 -and $_.CreationTime.Date -eq $ThisDay){  # What about non-zero length files?
             $TodaysFiles++
             Copy-Item "E:\Source\*.txt" -Destination $DestDirectory
         }
     }
    
 if (-not $TodaysFiles){
     Clear-Host
#Dont find any file from today
$Body = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
body, h1 {
	font-family:Arial, Helvetica, sans-serif
}
</style>
<title>Report</title>
</head>

<body>
<h1>Daily Report</h1>
<table width="300" border="1">
  <tr>
    <td>Files found from today</td>
    <td>$TodaysFiles</td>
  </tr>
  <tr>
    <td>Old files found</td>
    <td>$OtherFiles</td>
  </tr>
</table>

</body>
</html>
"@
     Write-Host $body
     Send-MailMessage -To forename.aftername@domain.com -From server@domain.com -Subject "Something wrong" -BodyAsHtml $body -SmtpServer smtp1.test.domain.com
 }
 #Found old files and files from today
 elseif($OtherFiles){
     Clear-Host
     $Body = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
body, h1 {
	font-family:Arial, Helvetica, sans-serif
}
</style>
<title>Report</title>
</head>
<body>
<h1>Daily files</h1>
<table width="300" border="1">
  <tr>
    <td>Files found from today</td>
    <td>$TodaysFiles</td>
  </tr>
  <tr>
    <td>Old files found</td>
    <td>$OtherFiles</td>
  </tr>
</table>

</body>
</html>
"@
     Write-Host $body
     Send-MailMessage -To forename.aftername@domain.com -From server@domain.com -Subject "Something wrong" -BodyAsHtml $body -SmtpServer smtp1.test.domain.com 
 }
#Found files from today
 else{
     Clear-Host
$Body = @"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
body, h1 {
	font-family:Arial, Helvetica, sans-serif
}
</style>
<title>Report</title>
</head>

<body>
<h1>Daily files</h1>
<table width="300" border="1">
  <tr>
    <td>Files found from today</td>
    <td>$TodaysFiles</td>
  </tr>
  <tr>
    <td>Old files found</td>
    <td>$OtherFiles</td>
  </tr>
</table>

</body>
</html>
"@
     Write-Host $body
     Get-ChildItem $DestDirectory | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} | 
     send-mailmessage -from "Reporting <server@domain.com>" -to "Forename Aftername <forename.aftername@domain.com>" -subject "Daily Reports" -SmtpServer smtp1.test.domain.com -BodyAsHtml $Body

 }

But the recipient only receive some html code in the a-mail message.

What could probably be the problem. I´ve dived in s lot of google examles but now I have to
give up and try if this forum can tell me whats wrong.

Maybe I 've stared at this solution to long
so I cant see whats wrong.

Really appreciate your answer

Update:
The recipient i using Google (Google Suite Enterprises) as mail client.

Please carefully (re-)read the help for the cmdlet Send-MailMessage

Please read it completely including the examples to learn how to use it … especially the help for the parameters -Body and -BodyAsHtml!! :wink:

This code worked!

send-mailmessage -from "Report server <server@domain.com>" -to "Forname Aftername <forname.aftername@domain.com>" -subject "Report" -SmtpServer smtp1.test.domain.com -BodyAsHtml $Body

Great. I’m glad it’s been helpful. :+1:t4:

Anyway … you may still have a missconception about how it works though. The parameter -BodyAsHtml is a switch parameter and does not expect any value. :point_up_2:t4:
To make that clear you should write the command like this:

Send-MailMessage -From "Report server <server@domain.com>" -To "Forname Aftername <forname.aftername@domain.com>" -Subject "Report" -SmtpServer smtp1.test.domain.com -Body $Body -BodyAsHtml

To make your code a lot easier to read you may consider about using splatting: