;; 
;;=====================================================================================----- 
;; 
;;FUNCTION        SendMail() 
;; 
;;ACTION          Sends an email through a local or specified smtp server using CDO 
;; 
;;AUTHOR          Allen Powell 
;; 
;;CONTRIBUTORS    Shane Ptomey 
;; 
;;VERSION         1.0 - 2011/04/18 
;; 
;;SYNTAX          SendMail(to,from [,subject] [,body] [,body type] [,attachment] 
;;                [,smtp server] [,port number] [,server req ssl] [,smtp user] [,smtp pw] 
;;                [,auth method] [,send method] [,timeout in secs])  
;; 
;;PARAMETERS      To - Required - String 
;;                - E-mail address to send message to. 
;;                Multiple email addresses can be defined by using a semicolon to separate 
;;                the addresses. For example: "email1@domain.com;email2@domain.com." 
;;                To use CC: or BCC:, place the prefix in front of the email address 
;;                For Example:  "cc:email1@domain.com;bcc:email2@domain.com." 
;; 
;;                FROM - Required - String 
;;                E-mail to display in the From field (some SMTPs require real email 
;;                address, some don't). 
;;                Note:  Can also provide "Friendly Name" to go along with email address. 
;;                For example:  "Your Name<youremail@domain.com>". 
;; 
;;                SUBJECT - Optional - String 
;;                - The desired subject of sent e-mail. 
;; 
;;                BODY - Optional - String 
;;                - The desired message to be included in e-mail. 
;;                Note: If bodytype is set to txtfile or htmlfile, then this should 
;;                specify path to body file. 
;; 
;;                BODYTYPE - Optional - String 
;;                the type of body, plain text, html, txt file, html file, 
;;                defaults to plain text. 
;;                1 or 'text' designates plain text message. 
;;                2 or 'html' designates inline html message. 
;;                3 or 'textfile' designates message will be from txt file 
;;                  specified in BODY parameter. 
;;                4 or 'htmlfile' designates message will be from html file 
;;                  specified in BODY parameter. 
;; 
;;                ATTACHMENT - Optional - String 
;;                - The full path to file that you want to attach to e-mail 
;; 
;;                SMTPSERVER - Optional - String 
;;                - The address of the smtp server to use (ie.. smtp.gmail.com) 
;;                NOTE: If SMTPSERVER is not provided it will attempt to send the message using 
;;                the local SMTP Service, (which must be installed). 
;; 
;;                SMTPSERVERPORT - Optional - Integer 
;;                - The port number used to send e-mail, defaults to port 25 when 
;;                specifying SMTPSERVER  
;;                NOTE:  Some ISPs block port 25, it may be necessary to use a different 
;;                port and/or SSL.  
;; 
;;                SMTPSERVERREQSSL - Optional - String 
;;                - 1 or 'true' or 't' designates that SMTP server requires a secure connection. 
;;                This defaults to false when specifying SMTPSERVER. 
;; 
;;                SMTPAUTHUSER - Optional - String 
;;                - The username used to authenticate to SMTP server. 
;; 
;;                SMTPAUTHPW - Optional - String 
;;                - The password associated with smtpauthuser used to authenticate to smtp server. 
;; 
;;                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 
;; 
;;EXAMPLES         
;; 
;;            ; Send a plain text email using a local SMTP/Exchange Server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','Body')  
;; 
;;            ; send a plain text email using gmail's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','Body',"Text",,'smtp.gmail.com',465,1,'login@gmail.com','password')  
;; 
;;            ; send a plain text email using hotmails's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','Body',"Text",,'smtp.live.com',25,1,'login@hotmail.com','password')  
;; 
;;            ; send a plain text email using yahoo's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','Body',1,,'smtp.mail.yahoo.com',465,1,'login@yahoo.com','password')  
;; 
;;            ; send an html email using gmail's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','<b>bold body</b><br><i>italiced body</i>',"HTML",,'smtp.gmail.com',465,1,'login@gmail.com','password')  
;; 
;;            ; send an email from a txt file using gmail's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','.\textfile.txt',"TextFile",,'smtp.gmail.com',465,1,'login@gmail.com','password')  
;; 
;;            ; send an email from an html file using gmail's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','.\htmlfile.htm',"HTMLFile",,'smtp.gmail.com',465,1,'login@gmail.com','password')  
;; 
;;            ; send a plain text email with attachment using gmail's smtp server  
;;            $rc=sendmail('to-email@domain.com','from-email@domain.com','Subject','Body',1,'.\filetoattach.txt','smtp.gmail.com',465,1,'login@gmail.com','password')  
;;  
;  
Function Sendmail($_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)
 
  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 
  $Sendmail       = 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