;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       SendMailBA() 
;; 
;;ACTION         Sends mail from an array using BLAT.EXE 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.1  - 2019/09/16 
;; 
;;HISTORY        1.0  - 2016/02/16 
;;               1.1  - 2019/09/16 - Enhancements to body processing and attachment support 
;; 
;;SYNTAX         SendMailBA(MessageData) 
;; 
;;PARAMETERS     MessageData - REQUIRED, Array - The array of data representing the. 
;;               message. It must use the following format: 
;;                
;;                0 SMTP_Server         = FQDN or IP of the SMTP Server authorized to send 
;;                1 SMTP_Port           = Port to communicate with SMTP service (default=25) 
;;                2 User                = The username, if required for authorization 
;;                3 Password            = The password, if required for authorization 
;;                4 HtmlFormat          = Boolean - set to 1 to use HTML Formatting 
;;                5 RecipientAddr       = The "To" email address - name@domain.tld 
;;                6 RecipientCC         = email addresses added to the "CC" list 
;;                7 RecipientBCC        = email addresses added to the "BCC" list 
;;                                        Above address lists can be comma delimited 
;;                8 FromAddr            = The "From" email address - myname@domain.tld 
;;                9 FromName            = The "From" email user name - Jane Sender 
;;               10 Subject             = The "Subject" line content 
;;               11 Message body        = text or html, CRLF or \n delimited, or can be 
;;                                        "FILE:<filespec>" 
;;               12 Attachment          = filespec to attach (comma delimited) 
;; 
;;                
;;               The username and password are only required if the server requires 
;;               authentication. If it does, care should be taken to secure the credentials. 
;; 
;;REMARKS        This function REQUIRES that NoMacrosInStrings be used or "@" signs be escaped! 
;;               If the message contains multiple lines, it will be written to a temp file in  
;;               the user's TEMP folder. The file will be removed upon exiting the function. 
;; 
;;RETURNS        Array of 3 elements 
;;               0 = Result (1=success, 0=fail) 
;;               1 = Error Code 
;;               2 = Error Message 
;;               3 = STDOUT result from BLAT command 
;;               4 = STDERR result from BLAT command 
;; 
;;DEPENDENCIES   BLAT.exe - must be in same directory as the calling script or in a folder 
;;               specified by the S_BIN environment variable. 
;; 
;;TESTED WITH    Servers: W2K-W2K12r2  Workstations: W2K, XP, Vista, Windows 7, 8, 10 
;; 
;;EXAMPLES       $ = SetOption('NoMacrosInStrings', 'On')	; REQUIRED to avoid issues with "@" signs 
;; 
;;               Dim $aMessageData[11] 
;;               $aMessageData[0]  = 'SMTPserver.domain.tld'	; SMTP Server IP or FQDN 
;;               $aMessageData[1]  = '587'			; Port Number 
;;               $aMessageData[2]  = 'AuthUser@domain.tld'	; SMTP Auth User 
;;               $aMessageData[3]  = 'P@sswurd'			; SMTP Auth Password 
;;               $aMessageData[4]  = 'N'			; Boolean - Use HTML message format 
;;               $aMessageData[5]  = 'JohnDoe@somewhere.tld'	; To (comma-delimited list) 
;;               $aMessageData[6]  = ''				; CC (comma-delimited list, can be blank) 
;;               $aMessageData[7]  = ''				; BCC (comma-delimited list, can be blank) 
;;               $aMessageData[8]  = 'sender@senddom.tld'	; From (address) 
;;               $aMessageData[9]  = 'Mary Q. Sender'		; From Name (full name) 
;;               $aMessageData[10] = 'Test Message'		; Subject (can be blank) 
;;               $aMessageData[11] = 'This is Line 1\n'		; Message Body (can be multi-line or use "\n") 
;;               $aMessageData[11] = $aMessageData[11] + 'This is Line 2' + @CRLF + @CRLF 
;;               $aMessageData[11] = $aMessageData[11] + 'This is Line 4' 
;;               $aMessageData[12] = 'C:\Temp\Report.pdf'	; attachment (comma-delimited list, can be blank) 
;;                
;;               $aResult = SendMailBA($aMessageData)		; call the function 
;;               If $aResult[0] 
;;                 'Success!' ? 
;;               Else 
;;                 '    Result: ' $aResult[0] ? 
;;                 'Error Code: ' $aResult[1] ? 
;;                 'Error Text: ' $aResult[2] ? 
;;                 '    STDOUT: ' $aResult[3] ? 
;;                 '    STDERR: ' $aResult[4] ? 
;;               EndIf 
; 
Function SendMailBA($_aMsgData)
 
  Dim $_Cmd						; command string 
  Dim $_To						; Recipient List 
  Dim $_Subj						; Subject line 
  Dim $_Fmt						; body format flag 
  Dim $_BlatCmd						; path to Blat.exe 
  Dim $_oExec						; Wsh Exec Object 
 
  ; fail if BLAT.EXE is not present in Script dir or System BIN dir 
  If Not Exist(@SCRIPTDIR + '\blat.exe') And Not Exist('%S_BIN%\blat.exe')
    $SendMailBA = 0,87,'Invalid Configuration - no BLAT.EXE file','',''
    Exit 87
  Else
    If Exist(@SCRIPTDIR + '\blat.exe')
      $_BlatCmd = @SCRIPTDIR + '\blat.exe'
    Else
      $_BlatCmd ='%S_BIN%\blat.exe'
    EndIf
  EndIf
 
  ; Determine if HTML or TEXT format - HTML overrides TEXT 
  $_Fmt  = IIf(InStr(' 1 T TRUE Y YES ON ', ' ' + $_aMsgData[4] + ' ') > 0, ' -html', '')
 
  ; Define the recipient (To) list, use "-ur" if blank and BCC is defined, error if blank and no BCC 
  If Not $_aMsgData[5]					; if To is empty 
    If $_aMsgData[7]					; and BCC is defined 
      $_To = ' -ur'					; set "Undisclosed Recipients" 
    Else
      $SendMailBA = 0,87,'Invalid Configuration - invalid recipients','',''
      Exit 87
    EndIf
  Else
    $_To = ' -to ' + $_aMsgData[5]
  EndIf
 
  ; Set the Subject line to "-ss" if blank 
  $_Subj  = IIf(Len($_aMsgData[10]) = 0, ' -ss', ' -subject "' + $_aMsgData[10] + '"')
 
  ; Check the Message body for multiple lines 
  If InStr($_aMsgData[11], @CRLF) Or InStr($_aMsgData[11], '\n')
    $_aMsgData[11] = Replace($_aMsgData[11], @CRLF, '|')
    $_aMsgData[11] = Replace($_aMsgData[11], '\n', '|')
  EndIf
 
  ; build the BLAT command to execute 
  $_Cmd = '%COMSPEC% /c ' + $_BlatCmd					; base command 
  If InStr($_aMsgData[11], 'File:')
    $_Cmd = $_Cmd + ' ' + Replace($_aMsgData[11], 'File:', '')          ; body from file 
  Else
    $_Cmd = $_Cmd + ' - -body "' + $_aMsgData[11] + '"'			; add Body text 
  EndIf
  $_Cmd = $_Cmd + $_Fmt							; add html format arg if defined 
  $_Cmd = $_Cmd + $_To							; Recipient list 
  If $_aMsgData[6]
    $_Cmd = $_Cmd + ' -cc ' + $_aMsgData[6]				; CC Recipient list if defined 
  EndIf
  If $_aMsgData[7]
    $_Cmd = $_Cmd + ' -bcc ' + $_aMsgData[7]				; BCC Recipient list if defined 
  EndIf
  $_Cmd = $_Cmd + ' -f "' + $_aMsgData[8] + '"'				; From Address 
  If $_aMsgData[9]
    $_Cmd = $_Cmd + ' -i "' + $_aMsgData[9] + '"'			; From Name 
  EndIf
  $_Cmd = $_Cmd + $_Subj						; subject 
  $_Cmd = $_Cmd + ' -server ' + $_aMsgData[0]				; SMTP server 
  If $_aMsgData[1]
    $_Cmd = $_Cmd + ' -port ' + $_aMsgData[1]				; SMTP Port 
  EndIf
  If $_aMsgData[2]
    $_Cmd = $_Cmd + ' -u ' + $_aMsgData[2]				; SMTP Auth ID (if specified) 
    If $_aMsgData[3]
      $_Cmd = $_Cmd + ' -pw ' + $_aMsgData[3]				; SMTP Auth PW (if specified) 
    EndIf
  EndIf
  If $_aMsgData[12]
    If Exist($_aMsgData[12])
      $_Cmd = $_Cmd + ' -attach ' + $_aMsgData[12]			; file attachment 
    EndIf
  EndIf
 
  $_oExec = CreateObject("WScript.Shell").Exec($_Cmd)			; instantiate WSH and run the command 
  If Not VarType($_oExec) = 9						; Check for correct vartype returned 
    $SendMailBA = 0,10,'WScript.Shell Exec Unsupported','',''		; set the result 
    Exit 10								; exit with error 
  Else
    ; Wait for completion and gather the STDOUT and STDERR text into arrays of arrays 
    While Not $_oExec.Status
      Sleep 0.01
    Loop
    $SendMailBA = Not($_oExec.ExitCode),$_oExec.ExitCode,@SERROR,$_oExec.StdOut.ReadAll,$_oExec.StdErr.ReadAll
    Exit = $_oExec.ExitCode
  EndIf
 
EndFunction