我有一个文本文件 output.txt ,其中包含以下内容:
OPERATINGSYSTEM PROJECTSERVER1 PROJECTSERVER2
Windows 1.36 4.42
Linux12 2.78 5.76
MacOS 3.45 6.39
Ubuntu 4.12 0.00
Android 0.00 3.46
FreePhysicalMemory 30.12 31.65
TotalVisibleMemorySize 48.00 48.00
CPULoadPercentage 2 4
我想在电子邮件中将 output.txt 的内容作为格式化HTML表格中的正文发送到 Windows server 2008 R2 。我正在尝试下面的代码,但它不适合我...我在哪里错误地在下面?
$smtpServer = "sunny@abc.com"
$smtpFrom = "process@abc.com"
$smtpTo = "administrator@abc.com"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content C:\Logs\output.txt
$style = "< style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "< /style>"
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
EDIT1
当我修改我的代码时如下:
$smtpServer = "sunny@abc.com"
$smtpFrom = "process@abc.com"
$smtpTo = "administrator@abc.com"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
$message.Body = "<head><pre>$style</pre></head>"
$message.Body += Get-Content C:\Logs\output.txt
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
我收到这样的邮件:
OPERATING SYSTEM SERVER1 SERVER2 Windows 1.36 4.42 Linux 2.78 5.76 MacOS 3.45 6.39 Android 0.00 3.46 Ubuntu 4.12 0.00 FreePhysicalMemory 30.12 31.65 TotalVisibleMemorySize 48.00 48.00
注意:我使用的是Powershell v1.0
答案 0 :(得分:4)
好的,这个问题有点不清楚,但我会根据我推断的“不适合你”来捅它。我猜你的脚本 成功发送了消息,而且无法正常工作的是消息格式化是完全错误的,原因有两个:
$message.IsBodyHTML = $true
将纯文本输入转换为HTML,但这不是它的作用。它只是设置消息的属性,指示邮件客户端将主体解释为HTML(具体来说,它将Content-Type标头设置为“text / html”),但您必须实际提供HTML代码。output.txt 应如下所示:
<body style="font-family: Arial; font-size: 10pt;">
<table style = "border: 1px solid black; border-collapse: collapse;">
<tr>
<th style = "border: 1px solid black; background: #dddddd; padding: 5px;">OPERATINGSYSTEM</th>
<th style = "border: 1px solid black; background: #dddddd; padding: 5px;">PROJECTSERVER1</th>
<th style = "border: 1px solid black; background: #dddddd; padding: 5px;">PROJECTSERVER2</th>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">Windows</td>
<td style = "border: 1px solid black; padding: 5px;">1.36</td>
<td style = "border: 1px solid black; padding: 5px;">4.42</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">Linux12</td>
<td style = "border: 1px solid black; padding: 5px;">2.78</td>
<td style = "border: 1px solid black; padding: 5px;">5.76</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">MacOS</td>
<td style = "border: 1px solid black; padding: 5px;">3.45</td>
<td style = "border: 1px solid black; padding: 5px;">6.39</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">Ubuntu</td>
<td style = "border: 1px solid black; padding: 5px;">4.12</td>
<td style = "border: 1px solid black; padding: 5px;">0.00</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">Android</td>
<td style = "border: 1px solid black; padding: 5px;">0.00</td>
<td style = "border: 1px solid black; padding: 5px;">3.46</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">FreePhysicalMemory</td>
<td style = "border: 1px solid black; padding: 5px;">30.12</td>
<td style = "border: 1px solid black; padding: 5px;">31.65</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">TotalVisibleMemorySize</td>
<td style = "border: 1px solid black; padding: 5px;">48.00</td>
<td style = "border: 1px solid black; padding: 5px;">48.00</td>
</tr>
<tr>
<td style = "border: 1px solid black; padding: 5px;">CPULoadPercentage</td>
<td style = "border: 1px solid black; padding: 5px;">2</td>
<td style = "border: 1px solid black; padding: 5px;">4</td>
</tr>
</table>
</body>
PowerShell脚本应如下所示:
$smtpServer = "sunny@abc.com"
$smtpFrom = "process@abc.com"
$smtpTo = "administrator@abc.com"
$messageSubject = "Servers Memory"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content C:\Logs\output.txt
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
如果您无法在HTML中提供 output.txt ,因为它是以纯文本格式自动生成的,那么您的脚本需要对其进行转换。如果格式始终如您的示例所示,那不是太复杂。将$message.Body =
行替换为:
$body = Get-Content C:\Logs\output.txt | Out-String
$body = $body -replace '^(\S+)\s+(\S+)\s+(\S+)', '<tr><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$1</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$2</th><th style = "border: 1px solid black; background: #dddddd; padding: 5px;">$3</th></tr>'
$body = $body -replace '\n(\S+)\s+(\S+)\s+(\S+)', '<tr><td style = "border: 1px solid black; padding: 5px;">$1</td><td style = "border: 1px solid black; padding: 5px;">$2</td><td style = "border: 1px solid black; padding: 5px;">$3</td></tr>'
$body = '<body><table style = "border: 1px solid black; border-collapse: collapse;">' + $body + '</table></body>'
$message.Body = $body