1

我有一个文本文件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的内容作为正文发送到Windows server 2008 R2中的格式化 HTML 表格中。我正在尝试使用下面的代码,但它对我不起作用..我在哪里弄错了?

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

编辑1

当我修改我的代码如下:

$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

4

1 回答 1

4

好的,这个问题有点不清楚,但我会根据我推断的“​​不适合你”来尝试一下。我猜您的脚本正在成功发送消息,而无法正常工作的是消息的格式都是错误的,原因有两个:

  1. output.txt是纯文本,而不是 HTML。在 HTML 中呈现时,不会保留空格,也不会有任何换行符。如果空格由空格组成,则连续空格将减少为单个空格,如果是制表符,我相信文本将全部一起运行,根本没有空格。我认为您希望$message.IsBodyHTML = $true将纯文本输入转换为 HTML,但这不是它的作用。它只是设置消息的一个属性,指示邮件客户端将正文解释为 HTML(具体而言,它将 Content-Type 标头设置为“text/html”),但您必须实际提供 HTML 代码。
  2. 您正在将$style变量设置为一堆 CSS 代码,但是您不使用它做任何事情。我想您打算将其添加到$message.Body。这样做的问题是您不能可靠地将样式表放入电子邮件中。大多数客户会忽略它。样式必须是内联的。

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
于 2013-10-29T04:41:47.650 回答