gpt4 book ai didi

powershell - 发送格式化的HTML电子邮件

转载 作者:行者123 更新时间:2023-12-03 00:52:31 24 4
gpt4 key购买 nike

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

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

最佳答案

好的,这个问题尚不清楚,但是我会根据我推断为“对您不起作用”的问题进行讨论。我猜想您的脚本已成功发送了消息,而行不通的是消息的格式全部错误,原因有两个:

  • output.txt 是纯文本格式,不是HTML。当以HTML呈现时,将不会保留空格,也不会有任何换行符。如果空格由空格组成,则连续空格将减少为单个空格,如果是制表符,我相信文本将全部一起运行而根本没有空格。我认为您期望$message.IsBodyHTML = $true将您的纯文本输入转换为HTML,但这不是这样做的。它只是设置了指示邮件客户端将正文解释为HTML的消息的属性(特别是,将Content-Type header 设置为“text / html”),但实际上必须提供HTML代码。
  • 您正在将 $ 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

    关于powershell - 发送格式化的HTML电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19648774/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com