0

my text file:

from: user1@example.com

to: user2@example.com, user3@example.com

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
Ella Ashr
  • 1
  • 1

1 Answers1

0

From Microsoft documentation.

Send-MailMessage

-To

The 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 someone@fabrikam.com.

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

user2@example.com, user3@example.com
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 user2@example.com, user3@example.com.

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

MS doc - Send-MailMessage

SO - How to Select-String multiline?

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39
  • Thanks, Sage a lot for your great answer that is very helpful, 1 more question, your code for the body section, does not skip "Body:" in the text file and sends the body including that word as it is, How can I fix that problem? for example in the recieved email I have: body:the text... – Ella Ashr Feb 24 '22 at 00:05
  • @EllaAshr I edited my answer but basically, you can add `.Substring(5).Trim()` to specifically remove the 5 first characters of that line, then trim any spaces remaining. – Sage Pourpre Feb 26 '22 at 04:14