[英]Send HTML Formatted Email to Multiple Recipients with Powershell
我有一个想扩展的PowerShell函数,但是遇到了一些功能障碍。
最终目标 :使用Windows PowerShell通过SMTP发送html格式的电子邮件给多个收件人。
条件 :
到目前为止的功能 :
Function Send-HTMLEmail
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpRecipientAddress,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpSubject,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpFromAddress = 'info@domain.com',
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$smtpServer = 'exchserver',
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$htmlFilePath,
[Parameter(ValueFromPipelineByPropertyName=$true)][string]$CSVFile
)
$smtpClient = new-object system.net.mail.smtpClient
$mailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = $smtpServer
$mailMessage.From = $smtpFromAddress
$mailMessage.To.add($smtpRecipientAddress)
$mailMessage.Subject = $smtpSubject
$mailMessage.IsBodyHtml = $true
$mailMessage.Body = $emailTemplate
$smtpClient.Send($mailMessage)
}
如您所见,该函数没有强制性参数。 这很容易添加,但是我不确定如何将CSVFile参数指定为必需参数,还是所有其他参数。
以下是html外观的大致示例。 如何导入和初始化PowerShell变量? (请注意,以下电子邮件中的变量将从另一个脚本中导入,这是不相关的):
<!DOCTYPE html> <html> <head> <style> body { background-color: #DEE8F1; } p.normal { font-family: "Calibri"; color: black; font-size: 13px; } p.normal em{ font-family: "Calibri"; color: black; font-size: 13px; font-weight: bold; } </style> </head> <body> <p class="normal"><em>$($user.Name)</em>,<br /><br /> Your password will expire in <em>$($userTable.DaysTillExpiry)</em> days on <em>$($userTable.ExpiryDate)</em><br /><br /> Your domain password is required for Computer Login, remote VPN, and Email Access.<br /><br /> To change your password, press CTRL-ALT-DEL and choose Change Password.<br /><br> For your password to be valid it must be 8 or more characters long and<br /> contain a mix of the following properties:<br /><br /> uppercase letters (AZ)<br /> lowercase letters (az)<br /> numbers (0-9)<br /><br /> Regards,<br /><br /><br /> <em>IT Department</em> <br /> Ph: +xx xxxx xxxx <br /><br /><br /> <br /></h1> </body> </html>
任何帮助将不胜感激!
好。 这是一个很大的。 不确定我是否能为您解决所有问题,但请尝试一下。 而且我将无法提供完整的脚本,只是给您提示如何解决它,好吗?
首先,为了使csvfile或所有其他参数成为必需参数,您应该阅读参数集。 基本上,您将csvfile分配给一个参数集,而其他所有参数都分配给第二个参数集。 然后,您可以将参数设置为强制性,用户将只能选择csvfile或其他参数。
其次,为了能够从外部文件读取html,并且仍然正确扩展变量,您需要像这样读取文件:
$mailBody = Get-Content $htmlFilePath | Foreach-Object {$ExecutionContext.InvokeCommand.ExpandString($_)}
祝好运!
PS> Get-Help Send-MailMessage -Parameter Body
-Body <String>
Specifies the body (content) of the e-mail message.
Required? false
Position? 3
Default value None
Accept pipeline input? false
Accept wildcard characters? false
好的,因此您需要一个String才能传递给-Body。
Function Get-HTMLvar
{
[PSCustomObject]
$user = @{ Name = 'JaneDoe' }
[PSCustomObject]
$userTable = @{ DaysTillExpiry = '23'
ExpiryDate = '2014/11/15' }
$user + $userTable
}
Function Get-HTMLbody
{
[CmdletBinding()]
Param
(
#$user Object
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
$user,
#$userTable Object
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
$userTable
)
$emailTemplate = "
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #DEE8F1;
}
p.normal {
font-family: `"Calibri`";
color: black;
font-size: 13px;
}
p.normal em{
font-family: `"Calibri`";
color: black;
font-size: 13px;
font-weight: bold;
}
</style>
</head>
<body>
<p class=`"normal`"><em>$($user.Name)</em>,<br /><br />
Your password will expire in <em>$($userTable.DaysTillExpiry)</em> days
on <em>$($userTable.ExpiryDate)</em><br /><br />
Your domain password is required for Computer Login, remote VPN,
and Email Access.<br /><br />
To change your password, press CTRL-ALT-DEL and choose Change Password.<br /><br>
For your password to be valid it must be 8 or more characters long and<br />
contain a mix of the following properties:<br /><br />
uppercase letters (A-Z)<br />
lowercase letters (a-z)<br />
numbers (0-9)<br /><br />
Regards,<br /><br /><br />
<em>IT Department</em> <br />
Ph: +xx xxxx xxxx <br /><br /><br />
<br /></h1>
</body>
</html>"
Write-Output $emailTemplate
}
Function Send-HTMLemail
{
[CmdletBinding()]
Param
(
[Parameter()]
[string]$smtpRecipientAddress = 'Rcpt@domain.local',
[Parameter()]
[string]$smtpSubject = 'Subject',
[Parameter()]
[string]$smtpFromAddress = 'Admin@domain.local',
[Parameter()]
[string]$smtpServer = 'relay.domain.local',
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]$emailTemplate
)
Send-MailMessage -From $smtpFromAddress `
-To $smtpRecipientAddress `
-SmtpServer $smtpServer `
-Body $emailTemplate `
-BodyAsHtml `
-Subject $smtpSubject `
-Verbose
}
所以,
PS> Get-HTMLvar | Get-HTMLbody | Send-HTMLemail
会给你
JaneDoe,
您的密码将于2014/11/15在23天内过期
您的域密码是计算机登录,远程VPN和电子邮件访问所必需的。
[..]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.