Reputation: 35
In the script below, it allows me to send many seperate emails to collegues since each collegue has seperate reports. Currently each collegue gets two files, with the future possibility of more. The code below sends multiple emails that uses a CSV file list to match email to file name(s).
Question: I am looking for a way to attach multiple files in one email via powershell; I am wondering if this could be adjusted to allow it to happen?
$File = $FileFolder+$Email.Filename
In the CSV file, which is created in excel, column A header is “Email” and column B header is “Filename”. Column A has the normal email address list and then the file names are all .PDF files that reside in the folder path noted below.
Code below
#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
Upvotes: 0
Views: 4140
Reputation: 35
solved it by reading other examples, basically learning as I go
solved it by changing File, to (File1, File2), then updating attachment loop, thanks again
#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
Upvotes: 1