;;
;;=====================================================================================-----
;;
;;FUNCTION SendMailBA()
;;
;;ACTION Sends mail from an array using BLAT.EXE
;;
;;AUTHOR Glenn Barnas
;;
;;VERSION 1.0 - 2016/02/16
;;
;;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 User = The username, if required for authorization
;; 2 Password = The password, if required for authorization
;; 3 HtmlFormat = <Boolean - set to 1 to use HTML Formatting>
;; 4 Reserved for Future Use
;; 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>
;; 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, can span multiple lines, including blank lines.
;;
;;
;; The username and password are only required if the server requires
;; authentication. If it does, the file should be restricted to
;; administrators, SYSTEM, (Modify) and the user running the script (Read).
;;
;;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] = 'AuthUser@domain.tld' ; SMTP Auth User
;; $aMessageData[2] = 'P@sswurd' ; SMTP Auth Password
;; $aMessageData[3] = 'N' ; Boolean - Use HTML message format
;; $aMessageData[4] = '' ; Reserved for Future Use
;; $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' + @CRLF ; Message Body (can be multi-line)
;; $aMessageData[11] = $aMessageData[11] + 'This is Line 2' + @CRLF + @CRLF
;; $aMessageData[11] = $aMessageData[11] + 'This is Line 4'
;;
;; $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 $_ ; general purpose var
Dim $_Cmd ; command string
Dim $_To ; Recipient List
Dim $_Subj ; Subject line
Dim $_Fmt ; body format flag
Dim $_Msg ; log message
Dim $_BlatCmd ; path to Blat.exe
Dim $_oExec ; Wsh Exec Object
Dim $_NMISState ; NoMacrosInStrings state
Dim $_TFile ; temp file for multi-line messages
; 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 O ON ', ' ' + $_aMsgData[3] + ' ') > 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 - use a temp file if so
$_TFile = ''
If InStr($_aMsgData[11], @CRLF)
$_TFile = '%TEMP%\BEMMsg_' + Right('00000' + @TICKS, 5) + '.msg'
$_ = RedirectOutput($_TFile, 1)
$_aMsgData[11]
$_ = RedirectOutput('')
EndIf
; build the BLAT command to execute
$_Cmd = '%COMSPEC% /c ' + $_BlatCmd ; base command
If $_TFile
$_Cmd = $_Cmd + ' ' + $_TFile
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
$_Cmd = $_Cmd + ' -i "' + $_aMsgData[9] + '"' ; From Name
$_Cmd = $_Cmd + $_Subj ; subject
$_Cmd = $_Cmd + ' -server ' + $_aMsgData[0] ; SMTP server
If $_aMsgData[1]
$_Cmd = $_Cmd + ' -u ' + $_aMsgData[1] ; SMTP Auth ID (if specified)
If $_aMsgData[2]
$_Cmd = $_Cmd + ' -pw ' + $_aMsgData[2] ; SMTP Auth PW (if specified)
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
If $_TFile Del $_TFile EndIf ; delete temp file, if used
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
If $_TFile Del $_TFile EndIf ; delete temp file, if used
Exit = $_oExec.ExitCode
EndIf
EndFunction