1

I have a script that runs against a directory and looks for files in it. Based on a portion of the filename containing the internal extension, it will then email the file to the associated user. I have it 90% working, it will email the correct person the correct file, but the variables in the email subject line are wrong. For example, a file intended for Bob goes to Bob, but the email subject line indicates Joe as well as the CallerID of Joe's call.

$Path = 'c:\test2'
$delim = '-'
$delim2 = '@'
$Port = "587"
$SMTPUsername = "[email protected]"
$EncryptedPasswordFile = "c:\test\voicemails.com.securestring"
$SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString
$EmailCredential = New-Object -TypeName Management.Automation.PSCredential($SMTPUsername,$SecureStringPassword )
$DestFolder = 'C:\test2\Processed'
#phone extensions & associated email address
$extensions = @{
  '1000'= '[email protected]';
  '23' = '[email protected]';
  '765'= '[email protected]'
    }
$File = Get-ChildItem $Path -Name
$File | Foreach-object {
    $FirstName = $null
    $nameArray = $_.Split($delim)
    $newName = $nameArray[2]+" "+($nameArray[0].substring(0,8))
    $ext = $nameArray[3]
    $callerID = $nameArray[2]
    $Datestamp = ($nameArray[0].substring(0,8))
    $emailAddress = $extensions[$ext]
    $FirstNameArray = $emailAddress.Split($delim2)
    $FirstName = $FirstNameArray[0]
     $SMTPMessage = @{
        To = $emailAddress
        From = '[email protected]'
        Subject = $Subject
        Smtpserver = '192.168.1.9'
        }
    $SMTPBody = 'Voicemail redirected message'
    $Subject = "Call Recording on $Datestamp from ($CallerID) to $FirstName"
    Send-MailMessage -Credential $EmailCredential @SMTPMessage -Body $SMTPBody -Attachments $_ -Port $Port
}

Some sample filenames I'm running it against:

  • 20180705072823-1530804495.22-15555552367-1000-Inbound.wav

  • 20180705072823-1530804495.22-15555551234-23-Inbound.wav

  • 20180705072823-1530804495.22-15555557791-765-Inbound.wav

I've tried to set the values to $null for each loop but doesn't make a difference.

1 Answer 1

2

This could happen because the hashtable is initialized before $subject is set.

$SMTPMessage = @{
    To = $emailAddress
    From = '[email protected]'
    Subject = $Subject # Uses old $subject
    Smtpserver = '192.168.1.9'
    }
# Set new subject
$Subject = "Call Recording on $Datestamp from ($CallerID) to $FirstName"

A simple example for illustration purposes:

$subject = "Subject 1"
$SMTPMessage = @{
    To = 'Rec 1'
    Subject = $subject
}
$SMTPMessage

$SMTPMessage = @{
    To = 'Rec 2'
    Subject = $subject
}
$subject = "Subject 2"
$SMTPMessage

Name                           Value
----                           -----
To                             Rec 1
Subject                        Subject 1
To                             Rec 2
Subject                        Subject 1 # Uses the value before reassignment
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, that's what it was. I removed the hashtable and just hardcoded the variables into the Send-MailMessage command and it is working as expected now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.