2017-10-13 31 views
0

在下面的脚本中,它允许我发送许多单独的电子邮件给同事,因为每个同事都有单独的报告。目前每个同事得到两个文件,未来的可能性更大。下面的代码会发送多封使用CSV文件列表的电子邮件,以将电子邮件与文件名进行匹配。Send-MailMessage从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 
+0

嗨,欢迎来到堆栈溢出。有关如何提出问题并更新您的问题的更多详细信息,请参见[问问]链接 。 –

+0

谢谢,你可以告诉我这个网站是新手,但之前已经访问过 - 我很熟悉编码。希望编辑使它更容易一些。 – John

回答

0

再解决它通过阅读其他的例子,基本上学习,我去

通过改变文件,以(文件1,文件2),解决它,然后更新连接环,感谢

#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