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.