'My PS below code sends only the first line of the body via email to only 1 recipient, how can I send multiple lines to multiple recipients
my text file:
from: [email protected]
to: [email protected], [email protected]
body: Please ignore any alerts and will inform you once the patching is completed.
Regards
Platform Team
my Powershell code as below:
$Subject = "Going to Start Patching " + $patchName + " Start Time:" + $startTime
$Content = "C:\Users\Documents\participants2.txt"
$From = (Select-String -Path $Content -Pattern "From:(.*)").Matches.Groups[1].Value
$To = (Select-String -Path $Content -Pattern "To:(.*)").Matches.Groups[1].Value
$Cc = (Select-String -Path $Content -Pattern "Cc:(.*)").Matches.Groups[1].Value
$Body = (Select-String -Path $Content -Pattern "Body:(.*)" -Context 0, 2).Matches.Groups[1].Value|
ForEach-Object {$_ -Replace 'patchname', $patchName}
$patchName = [System.NET.DNS]::GetHostByName('')
$startTime = Get-Date -Format "dddd MM/dd/yyyy HH:mm K"
$SMTPServer = ""
Send-MailMessage -From $From -To $To -Cc $Cc -SmtpServer $SMTPServer -Subject $Subject -Body $Body
Solution 1:[1]
From Microsoft documentation.
Send-MailMessage
-ToThe To parameter is required. This parameter specifies the recipient's email address. If there are multiple recipients, separate their addresses with a comma (,). Enter names (optional) and the email address, such as Name [email protected].
Let's highlight the important parts of your code here:
#...
$To = (Select-String -Path $Content -Pattern "To:(.*)").Matches.Groups[1].Value
#...
Send-MailMessage -From $From -To $To -Cc $Cc -SmtpServer $SMTPServer -Subject $Subject -Body $Body
Now consider the following
$To
$To.GetType()
The result will be
[email protected], [email protected]
String
This is your problem.
If you want to send to multiple recipients, you need to pass an array of string.
That being said, you are passing a single string (and so a single email, code-wise) with the value [email protected], [email protected].
To correct your issue, add .Split(',') | % {$_.Trim()} to your $To line.
$To = (Select-String -Path $Content -Pattern "To:(.*)").Matches.Groups[1].Value.Split(',') | % {$_.Trim()}
The .Split(',') will convert the string into an array, using the comma as delimiter and the | % {$_.Trim()} will trim the empty spaces before and after each email addresses, if any.
Now, if you try $To.GetType(), you will get a System.Array of Object[].
Your Send-MailMessage should work properly after that (if there is no other errors preventing it from working)
Additional Note:
Your $CC and $BCC (should you add one) will need the same .Split(',') treatment to convert them from String to array so that they are viewed as multiple email addresses by the Send-MailMessage cmdlet.
Regarding the body, you can use something like this to get the remaining lines...
$Body = ((Select-String -Path $Content -Pattern "Body:(.*)" -Context 0, 100)| ForEach-Object {
(, $_.Line + $_.Context.PostContext)
}) -join [System.Environment]::NewLine
$Body = ($Body -Replace 'patchname', $patchName).Substring(5).Trim()
Note that your replace won't do anything with the current message since "patchname" is not part of the template.
References
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
