简体   繁体   English

Powershell-发送电子邮件html

[英]Powershell - send email html

I have a code which check free space on disk and send email for me with alert, but I want that email was in html. 我有一个代码可以检查磁盘上的可用空间并通过警报为我发送电子邮件,但是我希望该电子邮件使用html。 What i do wrong? 我做错了什么?

This is my code: 这是我的代码:

$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) 

I get such a result in the email: 我在电子邮件中得到这样的结果:

<!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>

And not is converted to html, I dont know why? 而不是转换为html,我不知道为什么? I try converted to html $msg.Body = $MailTextT | ConvertTo-Html 我尝试转换为html $msg.Body = $MailTextT | ConvertTo-Html $msg.Body = $MailTextT | ConvertTo-Html . $msg.Body = $MailTextT | ConvertTo-Html

You shouldn't use the ConvertTo-Html on the text here as you've allready formatted your text. 您已经准备好格式化文本,因此不应在此处使用ConvertTo-Html。

Just use your html text directly (And read the contents as a whole using -raw): 只需直接使用html文本即可(并使用-raw整体读取内容):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM