Dear all,

I am trying to personalize a user written ado, winmail by Iain Soddy, ado and help file are available here.
This command serves to send emails via Stata.
I want to change it because I cannot use it currently.
I keep receiving an error message from powershell which tells me that my password is not crypted; therefore, I have to change only slightly the original ado (I have to create a secure string, then I have to crypt it and finally insert it in the PSCredential).

Based on this background, I have started to work on Soddy's ado.
I am completely new at both powersheel and programming in Stata; therefore, it took a several painful trials (and readings) before to get the the powershell script to work.
This is the changed powershell script (changes are written in red):

Code:
$smtpPort="587"
$smtpServer="smtp.gmail.com"
$user="MY SENDING EMAIL"
$from="MY SENDING EMAIL <MY SENDING EMAIL>"
$pass=Get-Content C:\COMPLETE_PATH\password.txt | ConvertTo-SecureString -AsPlainText -Force
$pass | ConvertFrom-SecureString | Out-File C:\COMPLETE_PATH\encrypt.txt
$to ="MY RECEIVING EMAIL"
$body1 = "hello friend"
$Body = "$body1"
$subject ="test"
$File= "C:\COMPLETE_PATH\encrypt.txt"
$cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,(Get-Content $File | ConvertTo-SecureString)
$bcc=""
$cc=""
$file = ""
Send-MailMessage -To $to -from $from -Subject $subject -SmtpServer $smtpServer -Port $smtpPort -Credential $cred -UseSsl -Body $Body -BodyAsHtml
If I run this code in powershell it works.

However, if I run the equivalent script in Stata it does not work: I do not receive any error message, but I think that some lines are just printed in powershell, without actually doing anything.
This is the powershall part of the Stata ado (changes are written in red):

Code:
    * Create the .ps1 file to mail
    file open mailps using `psloc'\mailps.ps1, text write replace

    file write mailps _n "$" `"smtpPort="`smtpport'""'    
    file write mailps _n "$" `"smtpServer="`smtpserver'""'
    file write mailps _n "$" `"user="`username'""'
    if "`from'"=="" file write mailps _n "$" `"from="`username'""'
    else file write mailps _n "$" `"from="`from' <`username'>""'    
   if "`cred'"!="" file write mailps _n "$" "pass=Get-Content " "`psloc'\pass.txt" " | ConvertTo-SecureString -AsPlainText -Force"
    else file write mailps _n "$" "pass=Get-Content " "`pfile'.txt" " | ConvertTo-SecureString -AsPlainText -Force"
    if "`cred'"!="" file write mailps _n "$" "pass " "| ConvertFrom-SecureString | Out-File " "`psloc'\encrypt.txt"
    else file write mailps _n "$" "pass " "| ConvertFrom-SecureString | Out-File " "encrypt.txt"
    file write mailps _n "$" `"to ="`to'""'
    
    if "`b'"!=""{
        forvalues i=1/`s(items)'{
            file write mailps _n "$" `"body`i' = "' `"""' "`body`i''" `"""'
        }
    }
    
    file write mailps _n "$" "Body = " `"""' "`html'" `"""'
    file write mailps _n  "$" "subject =" `"""' "`s'" `"""'
    if "`cred'"!="" file write mailps _n "$" "File= " "`psloc'\encrypt.txt"
    else file write mailps _n "$" "File= " "encrypt.txt"
    file write mailps _n "$" "cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList " " $" "user," "(Get-Content " " $" "File" "| ConvertTo-SecureString)"
    file write mailps _n "$" `"bcc="`bcc'""'        
    file write mailps _n "$" `"cc="`cc'""'        
    file write mailps _n  "$" `"file = "`att'""'
      
    local smmto "-To " "$" "to"
    local smmfrom "-from " "$" "from"
    local smmsmtps "-SmtpServer " "$" "smtpServer"
    local smmsmtpp "-Port " "$" "smtpPort"    
    local smmcred "-Credential " "$" "cred"
    
    if "`nossl'"!="" local smmssl ""
    else local smmssl "-UseSsl "
    if "`s'"=="" local smmsub ""
    else local smmsub "-Subject " "$" "subject"    
    if "`bcc'"=="" local smmbcc ""
    else local smmbcc "-Bcc " "$" "`bcc'"
    if "`cc'"=="" local smmbcc ""
    else local smmcc "-Cc " "$" "`cc'"    
    if "`att'"=="" local smmatt ""
    else local smmatt "-Attachments " "$" "`file'"            
    if "`html'"=="" local smmbody ""
    else local smmbody "-Body " "$" "Body -BodyAsHtml"
          
    file write mailps _n "Send-MailMessage `smmto' `smmfrom' `smmsub' `smmsmtps' "
    file write mailps "`smmsmtpp' `smmcred' `smmssl'" "`smmbcc'" "`smmcc'" "`smmatt'" "`smmbody'"
    
    file close mailps
This Stata ado generate the following powershell script:

Code:
$smtpPort="587"
$smtpServer="smtp.gmail.com"
$user="MY SENDING EMAIL"
$from="MY SENDING EMAIL <MY SENDING EMAIL>"
$pass=Get-Content password.txt | ConvertTo-SecureString -AsPlainText -Force
$pass | ConvertFrom-SecureString | Out-File encrypt.txt
$to ="MY RECEIVING EMAIL"
$body1 = "hello friend"
$Body = "$body1"
$subject ="test"
$File=encrypt.txt
$cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList$user,(Get-Content$File| ConvertTo-SecureString)
$bcc=""
$cc=""
$file = ""
Send-MailMessage -To $to -from $from -Subject $subject -SmtpServer $smtpServer -Port $smtpPort -Credential $cred -UseSsl -Body $Body -BodyAsHtml
I think that there are two types of problems in the lines written in blue--in particular with the paths: (i) paths to files password.txt and encrypt.txt are not complete, and (ii) there is not space before either $user or $File.
Stackoverflaw and a blog (just two examples I have found) suggest to use a combination of & and . to solve the two issues with the paths; I have tried this solution, but it does not work for me.

Does anyone have suggestions on how to solve the problems in the blue lines (assuming they are the only problems)?