WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Postie

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Loop thru local email accounts and forward to SMTP server

Keywords: 	 Loop thru local email accounts and forward to SMTP server

;***************************************************************************
;** 
;** PORTFWD.WBT by Bill Meek
;** 
;** Purpose: Loop thru local email accounts and forward to SMTP server on 
;**          another port. 
;** Inputs: INI file, multiple pop accounts
;** Outputs: SMTP ports from INI
;** Revisions:  
;***************************************************************************

;*****Changes to be made
;
;Look at optionally remove quoted printable marks ie - "=20" or "="
;   from HTML messages


;Load extenders
AddExtender("WWPST34I.DLL")
ver=kVerInfo(0)

;get working dir
currdir=DirGet() 
;attachment list (initalize)
fnlist=""        

ifile=StrCat(currdir, "portfwd.ini") 
If !FileExist(ifile)
   Message("Error", "Ini file does not exist")
   Exit
EndIf
;list of ini sections
ilist=IniItemizePvt("",ifile) 
;count of ini sections
icount=ItemCount(ilist,@TAB) 

;timing loop
While @TRUE   
   ;main loop
   ;loop number of INI entries
   for x = 1 to icount 
      ;get current entry
      centry=ItemExtract(x, ilist, @TAB) 
      ;get section names
      csects=IniItemizePvt(centry, ifile) 
      ; count sections
      ccount=ItemCount(csects,@TAB) 
      for c = 1 to ccount
         ;current item in section
         citem=ItemExtract(c, csects, @TAB) 
         ;read item val
         cval=IniReadPvt(centry, citem, "", ifile) 
         ;set as a variable
         %citem%=cval 
      next
   
   ; variables set from above: 
   ; pophost, popport, popuserid, poppassword
   ; smtphost, smtpport, smtpuserid, smtppassword
      while @true
         ;read email
         init1=kInit(pophost, StrCat("postie@", pophost), popuserid, poppassword, popport)
         ;****variable one is message number
         mvalue=kGetMail("1","temp.mail","","","t" ) 
         ;count of mail items
         mval=ItemExtract(3, mvalue, " ") 
         ;if no mail then break loop
         If mval == 0 then break 
      
         ;***Temp workaround for Postie not handling HTML messages
         ;***has the message delete in it***
         ;get raw message
         mvalue2=kGetMail("1","temp.raw","","","dr" ) 
         ;Wait for raw file to exist
         While !FileExist("temp.raw")
            TimeDelay(.5)
         EndWhile
         ;wait for filewrite to finish
         rawsize=FileSize("temp.raw")
         While rawsize == 0
            TimeDelay(.5)
            rawsize=FileSize("temp.raw")
         EndWhile
         ;Open file to read
         rawbuf=BinaryAlloc(rawsize*2)
         If rawbuf == 0
            Message("Error", "BinaryAlloc Failed")
            Exit
         EndIf
         ;Read the file into the buffer.
         BinaryRead(rawbuf, "temp.raw")
         ;See if contains HTML
         rawindicate=BinaryIndexEx(rawbuf, 0, "Content-Type: text/html;", @FWDSCAN, 0)
         If rawindicate == -1 
            ;set marker for not html
            ishtmlmessage=@FALSE 
         Else
            ;find beginning of HTML message
            rawhtmlbegin=BinaryIndexEx(rawbuf, rawindicate, "<", @FWDSCAN, 0)
            ;find end of HTML
            rawhtmlend=BinaryIndexEx(rawbuf, rawhtmlbegin, "------=_NextPart", @FWDSCAN, 0)
            If rawhtmlend == -1 then
               rawhtmlend=BinaryIndexEx(rawbuf, BinaryEodGet(rawbuf), ">", @BACKSCAN, 0)
            EndIf
            lenhtml=rawhtmlend-rawhtmlbegin
            htmlmessage=BinaryPeekStr(rawbuf, rawhtmlbegin, lenhtml)
            ;write out file
            rawpoked=BinaryPokeStr(rawbuf, 0, htmlmessage)
            BinaryEODSet(rawbuf, lenhtml)
            BinaryWrite(rawbuf,"out.raw")
            BinaryFree(rawbuf)
            ;set marker for html message
            ishtmlmessage=@TRUE 
         EndIf
   
         ;convert mvalue CRLF to LF for ease of use
         mvalue2=StrReplace(mvalue, @CRLF, @LF)
         ;set some variables from mvalue2 for later use
         msubject=StrSub(ItemExtract(2, mvalue2, @LF), 4, -1)
         mdate=StrSub(ItemExtract(3, mvalue2, @LF), 7, -1)
         ;get importance as text
         mimportance=StrSub(ItemExtract(4, mvalue2, @LF), 13, -1) 
         mfrom=StrSub(ItemExtract(5, mvalue2, @LF), 7, -1)
         ;find first bracket < 
         startbracket=StrScan(mfrom, "<", 1, @Fwdscan)
         If startbracket != 0
            endbracket=Strlen(mfrom)
            mfromaddress=StrSub(mfrom, startbracket+1, endbracket-startbracket-1)
         Else
            mfromaddress=mfrom
         EndIf
         mto=StrSub(ItemExtract(6, mvalue2, @LF), 5, -1)
         startbracket=StrScan(mto, "<", 1, @Fwdscan)
         If startbracket != 0
            endbracket=Strlen(mto)
            mtoaddress=StrSub(mto, startbracket+1, endbracket-startbracket-1)
         Else
            mtoaddress=mto
         EndIf
         
         If FileExist("temp.mail")
            ;check to see if the file is mime format
            fs=FileSize("temp.mail")
            binbuf=BinaryAlloc(fs+100)
            binhan=BinaryRead(binbuf, "temp.mail")
            binret=BinaryIndexEx(binbuf, 0, "This is a multi-part message in MIME format.", @FWDSCAN, 0)
            If binret != -1 ; if MIME multipart
               ;start at pos 46 and get to location of "Attachment: " -1
               bgattach=BinaryIndexEx(binbuf, 46, "Attachment: ", @FWDSCAN, 0)
               ;then get the data
               If bgattach == -1 
                  lenofmsg=BinaryEodGet(binbuf)-46
               Else
                  lenofmsg=bgattach-46-1
                  ;loop thru attachment list
                  startofattach=bgattach
                  While @true
                     ;get whole attachment line
                     nextlf=BinaryIndexEx(binbuf, startofattach, @LF, @FWDSCAN, 0)
                     If nextlf == -1 Then Break
                     attach=BinaryPeekStr(binbuf, startofattach, nextlf-startofattach)
                     ;get just the attachment filename
                     fnspace=BinaryIndexEx(binbuf, startofattach, " ", @FWDSCAN, 0)
                     fnparen=BinaryIndexEx(binbuf, startofattach, "(", @FWDSCAN, 0)
                     fnattach=BinaryPeekStr(binbuf, fnspace+1, fnparen-fnspace-1)
                     ;build attachment file list
                     If fnlist == ""
                        fnlist=fnattach
                     Else
                        fnlist=StrCat(fnlist, @TAB, fnattach)
                     EndIf
                     startofattach=nextlf+1
                  EndWhile
               EndIf
               newmsg=BinaryPeekStr(binbuf, 46, lenofmsg)
               ;Write out new output file from newmsg
               mailout = "out.mail"
               binbuf2=BinaryAlloc(lenofmsg+2)
               binhand2=BinaryPokeStr(binbuf2, 0, newmsg)
               BinaryWriteEx(binbuf2, 0, mailout, 0, lenofmsg)
               BinaryFree(binbuf)
               BinaryFree(binbuf2)
            Else
               FileCopy("temp.mail", "out.mail", @FALSE )
            EndIf
         EndIf
            
         ;handle delay for filewrite
         While !FileExist("out.mail")
            TimeDelay(.5)
         EndWhile
         While FileSize("out.mail") == 0
            TimeDelay(.5)
         EndWhile
   
         ;send email on other port
         init2=kInit(smtphost, mfromaddress, smtpuserid, smtppassword, smtpport)
         dest2=kDest(mtoaddress,"","")
         ;set priority of message, def to blank (Normal) if not set
         pflag=""
         If mimportance == "Normal" then pflag=""
         If mimportance == "High" then pflag="1"
         If mimportance == "Low" then pflag="2"
         If ishtmlmessage
            ;send as HTML messaage
            kretval=kSendFile(msubject,"out.raw",fnlist,StrCat("h",pflag)) 
         Else
            ;send as regular text
            kretval=kSendFile(msubject,"out.mail",fnlist,pflag)
         EndIf
         If kretval==0
            errline=kStatusInfo()
            Message("Message not sent", errline)
            Exit
         EndIf
      
         ;perform cleanup of files
         If FileExist("temp.mail") Then FileDelete("temp.mail")
         If FileExist("out.mail") Then FileDelete("out.mail")
         If FileExist("temp.raw") Then FileDelete("temp.raw")
         If FileExist("out.raw") Then FileDelete("out.raw")
         If fnlist != ""
            fncnt=Itemcount(fnlist, @TAB)
            If fncnt != 0 
               For d = 1 To fncnt
                  currfn=ItemExtract(d, fnlist, @TAB)
                If FileExist(currfn) Then FileDelete(currfn)
               Next
            EndIf
         EndIf
         ;empty list
         fnlist="" 
      EndWhile
   Next
;TimeDelay totaling 2 minutes
;Break into (24) 5 second incremnts to make exiting faster
For tdloop = 1 to 24
   TimeDelay(5)
   ;check for exit keys
   If IsKeyDown(@CTRL & @SHIFT)
      ;StrCat commands to make the display bigger and easier to notice
      Display (3, "Notice", StrCat(@CRLF,@CRLF,@CRLF,"          PortFwd Halted by User          ",@CRLF,@CRLF,@CRLF))
      Exit
   EndIf
Next
EndWhile      

Exit

Article ID:   W15414
File Created: 2017:07:28:12:58:36
Last Updated: 2011:01:12:08:30:50