Home > Blockchain >  send email with 1 or more attachments
send email with 1 or more attachments

Time:07-16

I have a script that will send up to 4 different attachments. The script will always send at least 1 attachment.

    Send-MailMessage -to $EmailTo -Body $Body -Subject "$TodayDate Report" -Attachments $UsersResultFileName,$NumbersTableFilename -From '[email protected]' -SmtpServer 'server' -port '25' -BodyAsHtml -ErrorAction Stop

Each variable in attachments can lead to a path and a file name.

The issue I am having is sometimes a variable is empty or null. When that happens the send-mail fails with an error saying an attachment is null and the error is not wrong.

I know I can if my way to all the combinations but there has to be another way?

CodePudding user response:

Santiagos works, but I think the following is more pretty

Send-MailMessage -to $EmailTo -Body $Body -Subject "$TodayDate Report" -Attachments ($UsersResultFileName,$NumbersTableFilename -ne $null) -From '[email protected]' -SmtpServer 'server' -port '25' -BodyAsHtml -ErrorAction Stop

Apparently boolean expressions work as filters with arrays

  • Related