【问题标题】:Send-MailMessage to Add Multiple Files from CSV ImportSend-MailMessage 以从 CSV 导入添加多个文件
【发布时间】:2017-10-13 03:20:44
【问题描述】:

在下面的脚本中,它允许我向同事发送许多单独的电子邮件,因为每个同事都有单独的报告。目前每个同事都有两个文件,未来可能会有更多。下面的代码发送多封电子邮件,这些电子邮件使用 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

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。请参阅How to Ask 链接以获取有关如何提出问题并相应更新您的问题的更多详细信息。
  • 谢谢,你可以告诉我我是这个网站的新手,但之前访问过 - 我对编码非常陌生。希望编辑让它变得更容易一些。

标签: powershell csv outlook


【解决方案1】:

通过阅读其他示例解决了这个问题,基本上是边走边学

通过将文件更改为(文件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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2020-01-30
    • 2017-04-13
    • 2021-09-18
    • 2015-05-03
    相关资源
    最近更新 更多