Quotes and emailing -SOLVED

by JonBryce at 2012-12-19 17:08:47

This has been driving me crazy…

I have a range of scripts, currently using hard-coded email addresses for any emails that they want to send out. For one address, the address is something like…


For multiple, I have the following


- and all works fine.

I’m trying to enhance this to read the email addresses from a flat file - so when someone leaves, I just change the file rather than half-a-dozen scripts. My flat file reader script works fine - I pass it a code (3-digit numeric) and I read the array that I filled with a get-Content 'til I match the code, then I have the string passed back with a "write-output". The calling script reads it …


and we’re in business.

But testing this has caused me hours of puzzlement. I’m reading back "jbryce@myplace.co.nz" but when I put it into the $EM_TOADDR string variable that I use in the Send-MailMessage, I get …

Send-MailMessage : The specified string is not in the form required for an e-mail address.

Oddly, when I swap the string $EM_TOADDR for a hard-coded email address "jbryce@myplace.co.nz", it works fine.

I’m convinced it’s to do with quotes (doubles and singles) but haven’t rumbled it yet. My last attempt was to read the file with an unquoted string, just jbryce@myplace.co.nz, and build it up in this way …

$EM_FINAL_TOADDR = ‘("’ + $EM_TOADDR + ‘")’

I still fail.

Someone been down this road before? I’m about to embark on another few hours of playing with quotes…

by nohandle at 2012-12-20 00:20:42

returns what?
by JonBryce at 2012-12-20 10:52:26
Not sure - and thanks for posting - but I know what’s going on. For a single address it works fine - the data I read needs to be unquoted, unbracketed. For multiple addresses I’m reading the data as a single string - and it needs to be an array. I’ll add some code to split the data up as I read it, reformat it into the right sort of thing and I’m in business.

Jakub, this may have been what you were thinking too. Thanks again!
by nohandle at 2012-12-20 11:12:29
Yes, I was trying to find out if you are passing the data as array of strings or like single string :slight_smile: is the issue solved?
by JonBryce at 2012-12-20 13:28:28
Confirmed - and amazingly easy. I store the data separated in the file, unquoted, unbracketed and read a line at a time. I then split the line up …


Send-MailMessage -from scriptman@myplace.co.nz -To $EM_FINAL_TOADDR -Subject "Testing" -Body "Hello" -SmtpServer -xxxxxx

Yet another case where if something is difficult, you’re not doing it right.

Thanks for the help.
by nohandle at 2012-12-21 03:09:54
I don’t get why you split. :slight_smile:

If the data are stored just as email addresses each on one line you can load the whole file as array and pass it as array on the output. Giving you ability to do this
<load the addresses as array> | foreach {Send-MailMessage -To $_ etc…}
or if you need to get just one item on position 1 in the file (indexed from 0 to comply with arrays) you should create a function that does it. If you think of better way to store the addresses in the future just reimplement the function with the same interface or better and you are good to go.

function Get-EmailFromFile
[string]$Path = "C:\temp\an.txt"
#get the items and return the last one from the array
#add one to comply with indexing from 0
(get-content $path -TotalCount (++$Position))[-1]
Get-EmailFromFile -Position 1

Btw: there is solved button to mark the topic as solved.