Quotes and emailing -SOLVED

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5369

    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...

    "jbryce@myplace.co.nz"

    For multiple, I have the following

    ("fbloggs@myplace.co.nz","jbryce@myplace.co.nz")

    – 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 ...

    $EM_TOADDR = & .\GET_EMSTRING.ps1 100

    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....

    Thanks

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

    ($EM_TOADDR).getType().fullname
    "-$EM_TOADDR-"

    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 🙂 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 ...

    $EM_FINAL_TOADDR = $EM_TOADDR.split()

    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. 🙂

    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
    | 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
    {
    param
    (
    [Parameter(Mandatory=$true)]
    [int]$Position,
    [string]$Path = "C:\temp\an.txt"
    )
    Process
    {
    #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.

You must be logged in to reply to this topic.