将附件和抄送参数添加到smtp电子邮件功能

时间:2015-11-03 14:45:43

标签: powershell sendmail

我的powershell脚本当前处理从命令行向用户列表发送电子邮件。我想在电子邮件中添加附件以及抄送列表,以便所有电子邮件都不会通过" To"邮件参数。这是我目前的职能。

function SendEmail
{  
$smtpServer = "smtp.server"
$smtpFrom = "PROD <email@gmail.com>" 
$smtpTo = "me@gmail.com"  
$messageSubject = "Weekly List "+$day
$messagebody = "Hi User, Please find attached document. Thank you."

$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messageSubject,$messagebody)
}

我的尝试是添加一个名为$ attachment =&#34; \ PathtoFile&#34;的变量。 并在发送功能中添加$附件,但这不起作用......

1 个答案:

答案 0 :(得分:1)

Send-MailMessageAttachmentsCc参数一起使用:

Send-MailMessage -Attachments "C:\path\to\attachment.ext" -Cc "myboss@gmail.com"

您还可以指定编码(您提到的特殊字符):

Send-MailMessage -Encoding UTF8

一般情况下,我会建议splatting参数,所以最终看起来像这样:

$MyAttachments = ".\a\relative\path.ext","C:\another\file.ext"

$MailParams  = @{
    SmtpServer = "smtp.server"
    From = "PROD <email@gmail.com>" 
    To = "me@gmail.com"  
    Subject = "Weekly List "+$day
    Body = "Hi User, Please find attached document. Thank you."
    Attachments = $MyAttachments
    Encoding = [System.Text.Encoding]::UTF8
}

Send-MailMessage @MailParams