电子邮件正文格式和修改

时间:2013-10-31 08:52:31

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

我在Powershell中使用以下代码从纯文本文件中获取正确表格格式的电子邮件 output.txt

code.ps1

$smtpServer = "sunny@domain.com"
$smtpFrom = "process@abc.com"
$smtpTo = "sunny@abc.com"
$messageSubject = "Data"

$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true

$body = Get-Content C:\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

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)

我想在接收电子邮件结构时进行以下更改:

CHANGE1:

电子邮件的格式如下表所示:

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

但是,如何将第2列和第3列的值仅在中间对齐,如下所示?

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

Change2:

我如何在电子邮件正文的表格之外添加以下注释:

All memory values are in GB.

Note: This is an automatically generated email, please don't reply to it.

Change3:

如何将此电子邮件发送给具有多个CC的多个收件人。我可以添加以逗号分隔的电子邮件ID,例如 $ smtpTo =“administrator @ abc.com,dbadmin @ abc.com ...” ..?

1 个答案:

答案 0 :(得分:2)

更改1 :从

更改表格行定义
$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 -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;text-align:center;">$2</td><td style = "border: 1px solid black; padding: 5px;text-align:center;">$3</td></tr>'

说明:将text-align:center添加到表格单元格的样式定义中。

更改2 :只需在邮件末尾添加文字:

$body = '<body><table style = "border: 1px solid black; border-collapse: collapse;">' + $body + '</table><br/>All memory values are in GB.<br/><br/>Note: This is an automatically generated email, please don't reply to it.<br/></body>'

更改3 :根据http://msdn.microsoft.com/en-us/library/vstudio/h1s04he7(v=vs.100).aspx,您是对的:添加多个以,分隔的电子邮件地址