;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       SendMailCA() 
;; 
;;ACTION         Sends mail from an array using CDO 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;CONTRIBUTORS   Based on code by Allen Powell & Shane Ptomey 
;; 
;;VERSION        1.0 - 2019/09/16 
;; 
;;HISTORY        1.0 - 2019/09/16 - Initial Release 
;; 
;;SYNTAX          SendMailCA(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 (comma delimited) 
;;                6 RecipientCC         = email addresses added to the "CC" list (comma delimited) 
;;                7 RecipientBCC        = email addresses added to the "BCC" list (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, can span multiple lines, including blank lines. 
;;               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. 
 
 
 
 
 
 
;; 
;;                SMTPSERVERREQSSL - Optional - String 
;;                - 1 or 'true' or 't' designates that SMTP server requires a secure connection. 
;;                This defaults to false when specifying SMTPSERVER. 
;; 
;;                SMTPAUTHMETHOD - Optional - Integer 
;;                - The type of authentication used to authenticate to SMTP server. Defaults to 
;;                basic security (2) when specifying SMTPSERVER. 
;;                1 or 'anonymous' or 'none' will designate no security. 
;;                2 or 'basic' or 'clear' will designate basic security. 
;;                3 or 'ntlm' will designate microsoft NT Lan Mgr security. 
;; 
;;                SENDUSING - Optional - String 
;;                - The send method desired. When specifying SMTPSERVER defaults to 
;;                network (2), otherwise defaults to local (1). 
;;                1 or 'local' designates that a local smtp pickup service will be used. 
;;                2 or 'network' designates that smtp will go over network. 
;; 
;;                TIMEOUT - Optional - Integer 
;;                - The desired timeout in secs that will be used when connecting to server. 
;;                Defaults to 60 when specifying SMTPSERVER. 
;; 
;; 
;;REMARKS         
;; 
;;RETURNS        Nothing 
;;               should return 1 if successful, 0 otherwise 
;;               Exits with appropriate error codes. 
;; 
;;DEPENDENCIES   CDO 
;; 
;;TESTED WITH    W2K3, W2K8, W2K12 
;; 
; 
;$_To, $_From, optional $_Subject, optional $_Body, optional $_Bodytype, optional $attachment, optional $_Smtpserver, optional $_Smtpserverport, optional $_SmtpserverreqSSL, optional $_SmtpauthUser, optional $_SmtpauthPW, optional $_Smtpauthmethod, optional $_Sendusing, optional $_Timeout  
Function Sendmail($_aMsgData)
 
  Dim $_Rc								; Misc and Return-Code vars 
  Dim $_Bodyformat							;  
  Dim $_Mailformat							;  
  Dim $_MailFormatMime							;  
  Dim $_MailFormatText							;  
  Dim $_BodyFormatHTML							;  
  Dim $_BodyFormatText							;  
  Dim $_Htmlbody							;  
  Dim $_Cdo,$_Fh							;  
  Dim $_Addresses							;  
  Dim $_Address								;  
  Dim $_Cc								;  
  Dim $_Cccount								;  
  Dim $_Bcc								;  
  Dim $_Bcccount							;  
  Dim $_ToCount								;  
 
  ; Initialize internal variables 
  $SendmailCA      = 1							; assume success! 
  $_MailFormatMime = 0
  $_MailFormatText = 1
  $_BodyFormatHTML = 0
  $_BodyFormatText = 1
 
  ; mask '@' if NoMacrosInStrings isn't set 
  If setoption('NoMacrosinStrings') = 'off'
    $_To = Join(Split($_To, '@'), '@@')
    $_From = join(split($_From, '@'), '@@')
    $_Smtpauthuser = Join(Split($_Smtpauthuser, '@'), '@@')
    $_SmtpauthPW = Join(Split($_SmtpauthPW, '@'), '@@')
  EndIf
 
  If InStr($_To, ':')
    $_Addresses = split($_To, ';')
    $_To = ''
    For Each $_Address In $_Addresses
      Select
       Case Left($_Address,3) = 'to:'
        ReDim Preserve $_To[$_ToCount]
        $_To[$_ToCount] = right($_Address, -3)
        $_ToCount = $_ToCount + 1
 
       Case left($_Address,3) = 'cc:'
        ReDim Preserve $_Cc[$_Cccount]
        $_Cc[$_Cccount] = right($_Address, -3)
        $_Cccount = $_Cccount + 1
 
       Case left($_Address,4) = 'bcc:'
        ReDim Preserve $_Bcc[$_Bcccount]
        $_Bcc[$_Bcccount] = right($_Address, -4)
        $_Bcccount = $_Bcccount + 1
 
       Case 1
        ReDim Preserve $_To[$_ToCount]
        $_To[$_ToCount] = $_Address
        $_ToCount = $_ToCount + 1
 
      EndSelect
 
    Next
 
    If $_ToCount
      $_To = Join($_To, ';')
    EndIf
 
    If $_Cccount
      $_Cc = Join($_Cc, ';')
    EndIf
 
    If $_Bcccount
      $_Bcc = Join($_Bcc, ';')
    EndIf
  EndIf
 
  Select
   Case $_Bodytype = 1 or $_Bodytype = 'Text'				; Text body 
    $_Bodyformat = $_BodyFormatText
    $_Mailformat = $_MailFormatMime
 
   Case $_Bodytype = 2 or $_Bodytype = 'HTML'				; HTML body 
    $_Htmlbody = 1
    $_Bodyformat = $_BodyFormatHTML
    $_Mailformat = $_MailFormatMime
 
   Case $_Bodytype = 3 or $_Bodytype = 'TextFile'			; Text from file 
    $_Bodytype = 'file'
    $_Bodyformat = $_BodyFormatText
    $_Mailformat = $_MailFormatMime
 
   Case $_Bodytype=4 or $_Bodytype = 'HTMLFile'				; HTML from file 
    $_Htmlbody=1
    $_Bodytype = 'file'
    $_Bodyformat = $_BodyFormatHTML
    $_Mailformat = $_MailFormatMime
 
   Case 1								;  Text body (default) 
    $_Bodyformat = $_BodyFormatText
    $_Mailformat = $_MailFormatMime
 
  EndSelect
 
  If $_Bodytype = 'file'
    $_Fh = freefilehandle()
 
    If Not Exist($_Body)
      $Sendmail = 0
        Exit 2
    EndIf
 
    If $_Fh <= 0
      $Sendmail = 0
      Exit @ERROR
    EndIf
 
    If open($_Fh, $_Body)<>0
      $Sendmail = 0
      Exit @ERROR
    EndIf
 
    $_Body=readline($_Fh)
    While Not @ERROR
      $_Body = $_Body + @CRLF + readline($_Fh)
    Loop
    $_Rc = Close($_Fh)
  EndIf
 
  $_Cdo=createobject('CDO.message')
 
  If $_Cdo
    $_Cdo.to      = $_To
    $_Cdo.cc      = $_Cc
    $_Cdo.bcc     = $_Bcc
    $_Cdo.from    = $_From
    $_Cdo.subject = $_Subject
 
    If $_Htmlbody
      $_Cdo.htmlbody = $_Body
    Else
      $_Cdo.textbody = $_Body
    EndIf
 
    $_Cdo.bodyformat = $_BodyFormat
    $_Cdo.mailformat = $_MailFormat
 
    If $attachment
      If Exist($attachment)
        $_Rc = $_Cdo.AddAttachment($attachment)
      Else
        $Sendmail = 0
        Exit @ERROR
      EndIf  
    EndIf
 
    Select
     Case $_Sendusing = 'local'
      $_Sendusing = 1  
     Case $_Sendusing = 'network'
      $_Sendusing = 2
     Case $_Smtpserver <> ''
      $_Sendusing=2
     Case $_Sendusing = '' and $_Smtpserver = ''
      $_Sendusing = 1
    EndSelect
 
    $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing").value = $_Sendusing
 
    If $_Smtpserver
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver").value = $_Smtpserver
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport").value = 25
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").value = 1	;basic/clear 
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").value = 60
    EndIf
 
    If $_Smtpserverport
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport").value = $_Smtpserverport
    EndIf
 
    If $_SmtpserverreqSSL
      If $_SmtpserverreqSSL = 'True' or $_SmtpserverreqSSL = 'T' or $_SmtpserverreqSSL = 1 
        $_SmtpserverreqSSL = not 0
      Else
        $_SmtpserverreqSSL = not 1
      EndIf
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl").value = $_SmtpserverreqSSL
    EndIf
 
    If $_Smtpauthuser and $_Smtpauthpw
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername").value = $_Smtpauthuser
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword").value = $_Smtpauthpw
    EndIf
 
    If $_Smtpauthmethod
      Select
       Case $_Smtpauthmethod = 'NTLM'
        $_Smtpauthmethod = 2
       Case $_Smtpauthmethod = 'Basic' or $_Smtpauthmethod = 'Clear'
        $_Smtpauthmethod=1
       Case $_Smtpauthmethod = 'Anonymous' or $_Smtpauthmethod = 'None'
        $_Smtpauthmethod=0
      EndSelect
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate").value = $_Smtpauthmethod
    EndIf
 
    If Val($_Timeout) > 0
      $_Cdo.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout").value = Val($_Timeout)
    EndIf 
 
    $_Cdo.Configuration.Fields.Update
    $_Cdo.Send
 
    If @ERROR
      $Sendmail = 0
      Exit @ERROR
    Else 
      $_Cdo = 0
      Exit 0
    EndIf
  Else
    $Sendmail = 0
    Exit @ERROR
  EndIf
 
EndFunction