Powershell - 发送电子邮件html

时间:2016-03-02 08:03:57

标签: html email powershell

我有一个代码可以检查磁盘上的可用空间,并通过警报为我发送电子邮件,但我希望该电子邮件是html。我做错了什么?

这是我的代码:

$html=
@'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
 <style type="text/css">
  .alert {{
    background-color: #FB1717 }}
  </style>
</head>
<body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>SystemName</th><th>DeviceID</th><th>VolumeName</th><th>Size(GB)</th><th>FreeSpace(GB)</th></tr>
{0}
</table>
</body></html>
'@

$entryTemplate = '<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'
$alertEntryTemplate = '<tr class="alert"><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6}</td></tr>'


Function Get-ComInfo {   
param(
$computers

)

#Here LOW Space thresold is lessthan 10% of the total size of the Volume
$PercentFree = @{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace /1GB)}} 
Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer $computers |
Select SystemName,DeviceID,VolumeName,$PercentFree,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{name="PercentFree(%)";Expression={int}}

}

$entries = Get-Content U:\Users\xxx\Desktop\servers.txt | % { Get-ComInfo -computers $_ } | % {

    if ([float]::Parse($_.'FreeSpace(GB)') -le 5) {
        $alertEntryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }

    else {
        $entryTemplate -f $_.SystemName, $_.DeviceID, $_.VolumeName, $_.'FreeSpace(GB)', $_.'Size(GB)', $_.'PercentFree(%)', $_.'LOW SPACE'
    }

}

$html -f ($entries -join ' ') | out-file U:\Users\xxx\Desktop\Drivess.htm

$EmailFrom = "xxx@xxx.com"
$EmailTo = "xxx@xxx.com" 
$Subject = "$computers DISK ISSUE DETECTED"  
$SMTPServer = "smtp.xxx.com" 
$msg = new-object Net.Mail.MailMessage  
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 

$SMTPClient.EnableSsl = $false 
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("XXX", "XXX"); 
$MailTextT =  Get-Content  -Path U:\Users\XXX\Desktop\Drivess.htm 
$msg.IsBodyHTML = $true 
$msg.Body = $MailTextT | ConvertTo-Html
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $MailTextT) 

我在电子邮件中收到了这样的结果:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>HTML TABLE</title>  <style type="text/css">   .alert {     background-color: #FB1717 }   </style> </head> <body> <table> <colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup> <tr><th>SystemName</th><th>DeviceID</th><th>VolumeName</th><th>Size(GB)</th><th>FreeSpace(GB)</th></tr> <tr><td>K005</td><td>C:</td><td></td><td>76,5</td><td>111,0</td><td></td><td></td></tr> <tr><td>K005</td><td>U:</td><td>Nowy</td><td>87,4</td><td>465,8</td><td></td><td></td></tr> </table> </body></html>

并没有被转换为HTML,我不知道为什么?我尝试转换为html $msg.Body = $MailTextT | ConvertTo-Html

1 个答案:

答案 0 :(得分:1)

您不应该在此处使用ConvertTo-Html,因为您已经格式化了文本。

直接使用你的html文本(并使用-raw作为整体阅读内容):

$MailTextT =  Get-Content  -Path U:\Users\XXX\Desktop\Drivess.htm -Raw
$msg.IsBodyHTML = $true 
$msg.Body = $MailTextT