发送格式化的HTML电子邮件

时间:2013-10-29 02:49:11

标签: powershell powershell-v2.0 powershell-v1.0

我有一个文本文件 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

1 个答案:

答案 0 :(得分:4)

好的,这个问题有点不清楚,但我会根据我推断的“​​不适合你”来捅它。我猜你的脚本 成功发送了消息,而且无法正常工作的是消息格式化是完全错误的,原因有两个:

  1. output.txt 是纯文本,而不是HTML。在HTML中呈现时,不会保留空白,也不会有任何换行符。如果空格由空格组成,则连续的空格将缩小为单个空格,如果是标签,我相信文本将全部一起运行,根本没有空格。我认为你期望$message.IsBodyHTML = $true将纯文本输入转换为HTML,但这不是它的作用。它只是设置消息的属性,指示邮件客户端将主体解释为HTML(具体来说,它将Content-Type标头设置为“text / html”),但您必须实际提供HTML代码。
  2. 您正在将 $ style 变量设置为一堆CSS代码,但之后您不会对其执行任何操作。我认为您打算将其添加到 $ message.Body 。问题在于您无法在电子邮件中可靠地放置样式表;大多数客户会忽略它。样式必须是内联的。
  3. 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