0

This might sound like a stupid question, but I would like to know how to import some mail addresses into PowerShell to send one mail to all of them like Send-MailMessage -To $MailTo.

I already managed to remove the text MailTo: in the beginning of the line. The example will make it more clear:

Input file:

# Comment
MailTo: User1@domain.com, User2@domain.com, User3@domain.com
MailTo: User4@domain.com, User5@domain.com, User6@domain.com, User7@domain.com, User8@domain.com

The code:

$File = (Get-Content -Path $ImportFile) -notlike  '#*' | Foreach {$_.TrimEnd()} | Where {$_ -ne ""}
$MailTo = $File | where { $_ -like "MailTo:*" } | Foreach {$_ -replace "^MailTo: "}

Solution:

$MailTo = ($File | where { $_ -like "MailTo:*" } | Foreach {$_ -replace "^MailTo: "}) -split ',\s*' | Foreach {$_.Trim()} | Where {$_}

Thanks to Joey below.

DarkLite1
  • 13,637
  • 40
  • 117
  • 214

2 Answers2

1

You can also use this variant, more robust:

$data = cat file
[regex] $re = "(?:\G(?!\A)|^MailTo:[ ]*)(?:[ ]*,[ ]*)?(?'mail'[^,\s]+)"
$address = $data | 
  select-string $re -all | 
       foreach {$_.matches}|foreach {$_.groups['mail'].value}
Write-output $address

For example if your $data contains:

# comment foo@bar.com, boo@domain.com
Mailto: mail@d.com, mail2@.col.col ...etc
Mailto: mailto@do.com 
Dummy@mail.com, mail3@goo.com
Data other data

It return exact data

walid toumi
  • 2,172
  • 1
  • 13
  • 10
  • Thank you walid, but what is the advantage of this regex in comparison to the other? – DarkLite1 Sep 24 '14 at 09:13
  • Thank you for the extra feedback Walid. The `Comment` section is already filtered out within the variale `$File`. So that's not something I worry about. I tested both solutions, the one in my OP and yours and they provide the same result. So both are ok I guess :) – DarkLite1 Sep 25 '14 at 07:40
0

So you essentially just want all mail addresses from that file? Or just from a particular line?

In the former case something like

$addresses = ((Get-Content $ImportFile) -replace '#.*|^MailTo:\s+') -split ',\s*'

should suffice. You can pass that array to the -To parameter of Send-MailMesssage. Perhaps there is still some stray whitespace in there afterwards, but that can easily be solved by piping the above to | %{$_.Trim()}.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Thank you for the help. But it doesn't work.. When I try `Send-MailMessage` to an array, only the last user receives the message, not everyone. – DarkLite1 Sep 24 '14 at 07:44
  • That's not what the documentation states, but assuming that it is indeed broken, that's trivial to work around. Just send one mail to each address, instead of one mail to all of them. – Joey Sep 24 '14 at 07:47
  • I actually need it to be one e-mail to multiple recipients. Same problem stated [here](http://stackoverflow.com/questions/10241816/powershell-send-mailmessage-email-to-multiple-recipients). I maybe need to build a `[String]` like `'User1@domain.com', 'User2@domain.com', ..`. – DarkLite1 Sep 24 '14 at 07:55
  • The author of that question explicitly converted the recipient array to string and passed that. I still doubt that sending a mail to multiple recipients does not work. – Joey Sep 24 '14 at 07:58
  • You're right, it does work when I manually fill it like `$addresses="User1@domain.com","User2@domain.com"`. But your proposal has a blank line as first line in there which might cause the issue. – DarkLite1 Sep 24 '14 at 08:06
  • Then add another `|?{$_}`. – Joey Sep 24 '14 at 08:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61828/discussion-between-darklite1-and-joey). – DarkLite1 Sep 24 '14 at 08:09