Send-MailMessage从CSV导入添加多个文件

时间:2017-10-13 03:20:44

标签: powershell csv outlook

在下面的脚本中,它允许我向同事发送许多单独的电子邮件,因为每个同事都有单独的报告。目前,每个同事都有两个文件,未来可能会有更多。下面的代码发送多封电子邮件,这些电子邮件使用CSV文件列表将电子邮件与文件名匹配。

问题: 我正在寻找一种通过PowerShell在一封电子邮件中附加多个文件的方法;我想知道是否可以调整它以允许它发生?

$File = $FileFolder+$Email.Filename

在excel中创建的CSV文件中,列A标题为“电子邮件”,列B标题为“文件名”。 A列具有正常的电子邮件地址列表,然后文件名是所有.PDF文件,它们位于下面提到的文件夹路径中。

以下代码

#Setup the location of the files to email
$FileFolder = "C:\Update\Files\"

#Logging File Location
$LogFile = "C:\Update\emaillog.txt"

#Import the csv file with filenames and email addresses
$csvfile = "C:\Update\email.csv"
$EmailList = IMPORT-CSV $csvfile

#Import the email content into the body variable
$Body = Get-Content "C:\Update\email.html" | out-string

#Setup the email template variables
$From = "email address removed"
$Subject = "Update Email"

# Step through each line in the csv file
ForEach ($Email in $EmailList) {
#Setup the two variables that change per email sent (To and File Attachment)
$To = $Email.Email
$File = $FileFolder+$Email.Filename

     #Send the mail message
Send-MailMessage -smtpServer outlook.iffo.com -from $From -to $To -subject $Subject -body $Body -attachment $File -BodyAsHTML
$LogTime = Get-Date -Format u
$LogMessage =  $LogTime+" Emailed "+$Email.Email+" the file "+$File
$LogMessage | Out-File $LogFile -Append
#Wait for a second before moving on to the next one, trying not to overwhelm the exchange server
    Start-Sleep -s 1

1 个答案:

答案 0 :(得分:0)

通过阅读其他示例解决了这个问题,基本上就像我一样学习

通过将File更改为(File1,File2),然后更新附件循环来解决它,再次感谢

#Setup the two variables that change per email sent (To and File Attachment)
    $To = $Email.Email
    $File1 = $FileFolder+$Email.Filename1
    $File2 = $FileFolder+$Email.Filename2

#Send the mail message

    Send-MailMessage -smtpServer outlook.info.com -from $From -to $To -subject $Subject -body $Body -attachment $File1, $File2 -BodyAsHTML