I have this powershell script to sending emails with attachments, but when I add multiple recipients, only the first one gets the message. I've read the documentation and still can't figure it out. Thank you
$recipients = "Marcel <[email protected]>, Marcelt <[email protected]>"
Get-ChildItem "C:\Decrypted\" | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname} |
send-mailmessage -from "[email protected]" `
-to "$recipients" `
-subject "New files" `
-body "$teloadmin" `
-BodyAsHtml `
-priority High `
-dno onSuccess, onFailure `
-smtpServer 192.168.170.61
This question is related to
powershell
That's right, each address needs to be quoted. If you have multiple addresses listed on the command line, the Send-MailMessage likes it if you specify both the human friendly and the email address parts.
for best behaviour create a class with a method like this
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddCommand("Send-MailMessage")
.AddParameter("SMTPServer", "smtp.xxx.com")
.AddParameter("From", "[email protected]")
.AddParameter("Subject", "xxx Notification")
.AddParameter("Body", body_msg)
.AddParameter("BodyAsHtml")
.AddParameter("To", recipients);
// invoke execution on the pipeline (ignore output) --> nothing will be displayed
PowerShellInstance.Invoke();
}
Whereby these instance is called in a function like:
public void sendEMailPowerShell(string body_msg, string[] recipients)
Never forget to use a string array for the recepients, which can be look like this:
string[] reportRecipient = {
"xxx <[email protected]>",
"xxx <[email protected]>"
};
this message can be overgiven as parameter to the method itself, HTML coding enabled!!
never forget to use a string array in case of multiple recipients, otherwise only the last address in the string will be used!!!
mail reportMail = new mail(); //instantiate from class
reportMail.sendEMailPowerShell(reportMessage, reportRecipient); //msg + email addresses
You must first convert the string to a string array, like this:
$recipients = "Marcel <[email protected]>,Marcelt <[email protected]>"
[string[]]$To = $recipients.Split(',')
Then use Send-MailMessage
like this:
Send-MailMessage -From "[email protected]" -To $To -subject "New files" -body "$teloadmin" -BodyAsHtml -priority High -dno onSuccess, onFailure -smtpServer 192.168.170.61
here is a full (gmail) and simple solution... just use normal ; delimiter.. best for passing in as params.
$to = "[email protected];[email protected]"
$user = "[email protected]"
$pass = ConvertTo-SecureString -String "pass" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $user, $pass
$mailParam = @{
To = $to.Split(';')
From = "IT Alerts <[email protected]>"
Subject = "test"
Body = "test"
SmtpServer = "smtp.gmail.com"
Port = 587
Credential = $cred
}
Send-MailMessage @mailParam -UseSsl
To define an array of strings it is more comfortable to use $var = @('User1 ', 'User2 ').
$servername = hostname
$smtpserver = 'localhost'
$emailTo = @('username1 <[email protected]>', 'username2<[email protected]>')
$emailFrom = 'SomeServer <[email protected]>'
Send-MailMessage -To $emailTo -Subject 'Low available memory' -Body 'Warning' -SmtpServer $smtpserver -From $emailFrom
Just creating a Powershell array will do the trick
$recipients = @("Marcel <[email protected]>", "Marcelt <[email protected]>")
The same approach can be used for attachments
$attachments = @("$PSScriptRoot\image003.png", "$PSScriptRoot\image004.jpg")
Source: Stackoverflow.com