Wednesday, 18 December 2024

Send Multiple Attachments Using Powershell

 

# Send Report on Email.
   $SMTPServer = "ServerName"
   $SMTPPort = "25"
   #Add email address of person in to & cc  whom you are sending email (Multiple addresses should be comma seperated)
   $to = "user@email.com"
   $subject = "$((Get-Date).GetDateTimeFormats()[0]) - Report Provisioning EXO E5"
   $message = New-Object System.Net.Mail.MailMessage
   $message.subject = $subject
   $message.body = "Report Provisioning EXO E5"
   $message.to.add($to)
   #$message.cc.add($cc)
   $message.IsBodyHtml = $True
   $message.from = "Report@email.com"
   $attachment = @("C:\Scripts\EXO-E5-LIC-Added.txt","C:\Scripts\$($Date)-RemoteUserMailbox.csv")
   #$attach_log = new-object Net.Mail.Attachment($attachment)

   foreach ($att in $attachment){

      $attach = new-object Net.Mail.Attachment($att)
      $message.Attachments.Add($attach)
   }

   $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
   $smtp.send($message)
   Write-Host `t -NoNewline
   Write-Host "Sending Email...: " -ForegroundColor White -NoNewline
   Write-Host "Email Sent" -ForegroundColor Green



No comments:

Post a Comment