Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Script that saves attachment
PostPosted: 2007-06-12 16:52 
New user
New user

Joined: 2006-10-04 14:28
Posts: 6
Hi,

no Warrenty at all, may someone can need the script:

add to the EventHander.vbs the following lines:

Script:
==============
Code:
Option Explicit

'***********************************************
'* Script zur automatischen Speicherung von Attachments
'***********************************************

Sub OnAcceptMessage(oClient, oMessage)
   ' Script   
   Dim fs                        ' Fielsystem Objekt
   Dim cPath                     ' Der Pfad wo alles bespeichert wird
   Dim attachCount               ' Counter für die Attachments
   Dim logger                    ' File in die das Logfile des Script geschrieben werden
   Dim dBug                      ' DBug Message ausgeben
   Dim DBugMeldung               ' Debug Meldungen
   Dim i,j,k,l                   ' Counter
   Dim msgFile                   ' Name der Email
   Dim logFile                   ' Name des Logfiles
   Dim doLogging                 ' Soll mit Geloggt werden
   Dim cMailDirectory            ' Directory in das die Attachments gespeichter werden
   Dim cConfiFile                ' File in dem beschrieben ist was bei welcher Email Adresse passiert
   Dim oFile                     ' FielsystemObjekt
   Dim cLine                     ' ConfigFile Zeile
   Dim aTemp                     ' Temporäres Array
   Dim aExtensionArray           ' Array falls verschiedene Exstesnions betrachtet werden müssen               
   ReDim aConfigArray(4,0)       ' AktionenProEmail
   Dim bExtensionArray           ' Berücksichtigende Erweiterungen?
   Dim bExtensionFound           ' Extension Found
   Dim cTempFileName             ' File name falls er schon vorhanden ist
   
   cPath = "D:\HMail\"
   cConfiFile = cPath & "Empfaengerliste.csv"
   logFile = "logFileHmailServer.txt"
   dBug = true
   Set fs = CreateObject("Scripting.FileSystemObject")
   
   If dBug Then
      Set DBugMeldung = fs.OpenTextFile(cPath & "Debug.txt", 8, True)
      DBugMeldung.Writeline("--- New Message ---")
      DBugMeldung.Writeline(Now()& " - Script Started")
   End If
   
   ' Config File einlesen:
   If dBug Then
      DBugMeldung.Writeline(Now() & " - The Following Config File will be readin:" & cConfiFile)
   End If
   
   Set oFile = fs.OpenTextFile(cConfiFile, 1, False)
   i = 0
   Do While oFile.AtEndOfLine <> True
      cLine = oFile.ReadLine
      If dBug Then
         DBugMeldung.Writeline(Now() & " - ReadLine: " & cLine) & "- Array Fields" & ubound(Split(cLine,";"),1)
      End If
      If Mid(cLine, 1, 1) <> "#" and 5 = ubound(Split(cLine,";"),1) Then
         
          If dBug Then
           DBugMeldung.Writeline(Now() & " - Correct Line Found: " & cLine)
         End If
         
         
         ReDim Preserve aConfigArray(4, i + 1)
         
          aTemp = Split(cLine,";")
         'Empfänger Adresse
         aConfigArray(0,i) = aTemp(0)
         
         ' Attachment Extension
         aConfigArray(1,i) = aTemp(1)
         
         'Logging
         aConfigArray(2,i) = aTemp(2)
         
         'Directory
         aConfigArray(3,i) = aTemp(3)

         'Overwrite Existing File
         aConfigArray(4,i) = aTemp(4)
         
         i = i + 1

      End If
   loop
   Set oFile = Nothing

   If dBug Then
      DBugMeldung.Writeline(Now() & " - Email List was Readin")
   End If
   
   msgFile = oMessage.Filename
   ' Nur Ausführen Falls Attachment vorhanden sind
   
   If oMessage.Attachments.Count > 0 Then
     
      If dBug Then
        DBugMeldung.Writeline(Now() & " - Attachment found")
      End If
     
      'it has attachments, so we'll take action 
      For j=0 to UBound(aConfigArray,2) - 1
         
         If dBug Then
            DBugMeldung.Writeline ""
            DBugMeldung.Writeline(Now() & " - Adresse: " & aConfigArray(0,j))
            DBugMeldung.Writeline(Now() & " - Extension: " & aConfigArray(1,j))
            DBugMeldung.Writeline(Now() & " - Logging: " & aConfigArray(2,j))
            DBugMeldung.Writeline(Now() & " - Directory: " & aConfigArray(3,j))   
            DBugMeldung.Writeline(Now() & " - Overwrite File: " & aConfigArray(4,j))   
            DBugMeldung.Writeline ""   
            DBugMeldung.Writeline(Now() & " - Anzahl Empfänger" &  oMessage.Recipients.Count)
         End If         
     
         For i = 0 To oMessage.Recipients.Count - 1
            If dBug Then
               DBugMeldung.Writeline(Now()& " - Compare Empfänger:" & aConfigArray(0,j) & " = " & oMessage.Recipients(i).Address & "== Ergebniss ==>" & InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ))
            End If

            If InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ) > 0 Then 
           
               If LCase(aConfigArray(2,j)) = "false" Then
                  doLogging = false
               Else
                  doLogging = true
               End if
                             
               ' In welches Directory muss die Emial gespeichert werden
               cMailDirectory = aConfigArray(3,j)
               If Len(Trim( cMailDirectory )) <= 0 Then
                  cMailDirectory = Replace(Replace(Replace(aConfigArray(0,j),"@","_at_"),".","_")," ","_") & "\"
               End If             
               
               If not(fs.FolderExists(cPath & cMailDirectory)) Then
                    fs.CreateFolder(cPath & cMailDirectory)
               End If
               
               ' Muss der Empafng Protokolliert werden             
               If doLogging Then
                  Set logger = fs.OpenTextFile(cPath & cMailDirectory & logFile, 8, True)
                  logger.WriteLine("===== Log Entry For: " & msgFile & " - " & Now() & "=====")
                  logger.WriteLine("Recipients Matches: " & aConfigArray(0,j))
               End If           
       
               
               'Extension Array Aufbereiten
               If Len(aConfigArray(1,j)) > 1Then
                  If InStr(1,aConfigArray(1,j),",") = 0 Then
                     aConfigArray(1,j) = aConfigArray(1,j) & ","
                  End If
                  aExtensionArray = Split(aConfigArray(1,j),",")
                  For k = 0 to UBound(aExtensionArray,1)
                     aExtensionArray(k) = Replace(aExtensionArray(k),"*","")
                     DBugMeldung.Writeline "xxxxx" & aExtensionArray(k)
                  Next
                  bExtensionArray = true
                  If doLogging Then
                      logger.WriteLine("Extension Check: True")
                  End If
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions vergleichen, Angaben - " & bExtensionArray)
                  End If
               Else
                  bExtensionArray = false
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions nicht vergleichen, keine Angaben - " & bExtensionArray)
                  End If
               End If
               
               attachCount = oMessage.Attachments.Count
                           
               For l = 0 To attachCount - 1
                 
                  If bExtensionArray Then
                     bExtensionFound = false
                     For k = 0 to UBound(aExtensionArray,1)-1
                         
                         If dBug Then
                              DBugMeldung.Writeline(Now()& " - Vergleich von " & oMessage.Attachments(i).Filename & " mit " & aExtensionArray(k))
                        End If

                        If InStr(1,Right(oMessage.Attachments(i).Filename,Len(aExtensionArray(k))),aExtensionArray(k),1) > 0 Then
                            bExtensionFound = true
                            If doLogging Then
                               logger.WriteLine("Extension gefunden: " & aExtensionArray(k))
                            End If
                            If dBug Then
                              DBugMeldung.Writeline(Now()& " - Extension gefunden: " & aExtensionArray(k))
                           End If
                        End If
                     Next
                  End If
                       
                  If not(bExtensionArray) or bExtensionFound Then
                     cTempFileName = cPath & cMailDirectory & oMessage.Attachments(i).Filename
                     If LCase(aConfigArray(4,j)) = "true" Then
                        oMessage.Attachments(i).SaveAs cTempFileName
                     Else
                        Do While fs.FileExists(cTempFileName)
                           If doLogging Then
                               logger.WriteLine("File Exist: " & cTempFileName)
                            End If
                      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cTempFileName&nbsp;=&nbsp;cPath&nbsp;&&nbsp;cMailDirectory&nbsp;&&nbsp;Replace(Replace(Replace(Now(),":","_"),".","_"),"&nbsp;","_")&nbsp;&&nbsp;"_"&nbsp;&&nbsp;oMessage.Attachments(i).Filename
                           If doLogging Then
                            logger.WriteLine("Try a new Filename: " & cTempFileName)
                         End If
                         If dBug Then
                            DBugMeldung.WriteLine("File Exist try a new Filename: " & cTempFileName)
                           End If
                        loop
                        oMessage.Attachments(i).SaveAs cTempFileName
                     End If
                     If doLogging Then
                        logger.WriteLine("*** Storing File --- filename: " & cTempFileName)
                        If dBug Then
                              DBugMeldung.Writeline(Now()& " - File  " & cTempFileName)
                        End If
                     End If
                  End If
               Next
               If doLogging Then
                  logger.Close()
                  Set logger = Nothing
               End If
            End If
         Next
      Next     
   End If
   
   If dBug Then
     DBugMeldung.Close()
     Set DBugMeldung = Nothing
   End If
   
   Set fs = Nothing
   
   ' set Result.Value = 0 so that the message is queued for delivery (according to hMailServer Docs
   Result.Value = 0
End Sub

==========================

Place in the cPath = "D:\HMail\" a CSV file Like:
Code:
# Felder die durch ";" getrennt sind
# 1) EmailAdresse (Pflichtfeld)
# 2) AttachmentEndung * = alle, ansonsten *.txt,*.zip (Pflichtfeld)
# 3) Logging true or false (Pflichtfeld)
# 4) Directory in welchen Directory es gepeichert werden muss. (Optional)
# 5) Overwrite existing files true or false (Pflichtfeld)
# Beispiele:
#maila@domain.de;.txt;true;Folder1\;true;
#maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;
#mailb@domain.com;*.txt,*.zip;false;;true;
# WICHTIG: AM Anfang keine ";" am Ende ein ";"
maila@domain.de;.txt;true;Folder1\;true;
maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;
mailb@domain.com;*.txt,*.zip;false;;true;

Christian


Top
 Profile  
 
 Post subject: Script that saves attachment
PostPosted: 2007-12-05 15:43 
New user
New user

Joined: 2007-12-05 15:27
Posts: 1
Hi,

Great script, works perfectly.
Just have a problem : when I sent more then 1 attachment it only saves the last attachment. I'm trying to find the cause of it myself, but the problem is that I'm not a developer.
I would be very happy if you (or someone else) could check this.


Regards,
Ptah


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2008-06-06 21:29 
New user
New user

Joined: 2008-06-03 15:59
Posts: 6
Indeed script saves the last attachment
What problems? somebody who knows?


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2008-09-23 10:37 
New user
New user

Joined: 2008-09-23 10:35
Posts: 1
Hi there, I get this message in the log file;

"ERROR" 2912 "2008-09-23 10:33:43.598" "Source: ScriptServer::LoadScripts, Code: HM10102, Description: File: C:\Program Files\hMailServer\Events\EventHandlers.vbs
Script Error: Source: Compilatiefout Microsoft VBScript - Error: 800A0400 - Description: Instructie wordt verwacht - Line: 200 Column: 22 - Code: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cTempFileName&nbsp;=&nbsp;cPath&nbsp;&&nbsp;cMailDirectory&nbsp;&&nbsp;Replace(Replace(Replace(Now(),":","_"),".","_"),"&nbsp;","_")&nbsp;&&nbsp;"_"&nbsp;&&nbsp;oMessage.Attachments(i).Filename"

Maybe there is some kind of syntax problem?

Regards, Thijs


[edit]

I just deleted the line, it seems to be working now. I wonder if it will still work when I use blank spaces in filenames...


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2008-12-31 10:20 
New user
New user

Joined: 2008-06-03 15:59
Posts: 6
Hi
somebody decided to issue the above script?
there are 2 problems:

1.if you send more than one attachment is stored only the first overwriting itself as many times as attachments sent.
logFileHmailServer.txt
Code:
===== Log Entry For: C:\Program Files\hMailServer\Data\{3331E546-9E04-4A0B-A48A-EA131F6185F8}.eml - 31.12.2008 4:59:46=====
Recipients Matches: info@domain.com
Extension Check: True
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml
*** Storing File --- filename: C:\GetMail\22\80020_4200000333_20081230_7591.xml
Extension gefunden: .xml


2. configuration does not take latest type of file to save, you need to write the last type of fake to first use.
Code:
# Felder die durch ";" getrennt sind

# 1) EmailAdresse (Pflichtfeld)
# 2) AttachmentEndung * = alle, ansonsten *.txt,*.zip (Pflichtfeld)
# 3) Logging true or false (Pflichtfeld)
# 4) Directory in welchen Directory es gepeichert werden muss. (Optional)
# 5) Overwrite existing files true or false (Pflichtfeld)
# Beispiele:
#maila@domain.de;.txt;true;Folder1\;true;
#maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;
#mailb@domain.com;*.txt,*.zip;false;;true;
# WICHTIG: AM Anfang keine ";" am Ende ein ";"
#maila@domain.de;.txt;true;Folder1\;true;
#maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;
#mailb@domain.com;*.txt,*.zip;false;;true;
info@domain.com;*.rar,*.xml,*.zip,*.asp;true;22\;true;     --> asp - fake


debug.txt
Code:
--- New Message ---
31.12.2008 9:59:15 - Script Started
31.12.2008 9:59:15 - The Following Config File will be readin:C:\GetMail\list.csv
31.12.2008 9:59:15 - ReadLine: # Felder die durch ";" getrennt sind- Array Fields1
31.12.2008 9:59:15 - ReadLine: # 1) EmailAdresse (Pflichtfeld)- Array Fields0
31.12.2008 9:59:15 - ReadLine: # 2) AttachmentEndung * = alle, ansonsten *.txt,*.zip (Pflichtfeld)- Array Fields0
31.12.2008 9:59:15 - ReadLine: # 3) Logging true or false (Pflichtfeld)- Array Fields0
31.12.2008 9:59:15 - ReadLine: # 4) Directory in welchen Directory es gepeichert werden muss. (Optional)- Array Fields0
31.12.2008 9:59:15 - ReadLine: # 5) Overwrite existing files true or false (Pflichtfeld)- Array Fields0
31.12.2008 9:59:15 - ReadLine: # Beispiele:- Array Fields0
31.12.2008 9:59:15 - ReadLine: #maila@domain.de;.txt;true;Folder1\;true;- Array Fields5
31.12.2008 9:59:15 - ReadLine: #maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;- Array Fields5
31.12.2008 9:59:15 - ReadLine: #mailb@domain.com;*.txt,*.zip;false;;true;- Array Fields5
31.12.2008 9:59:15 - ReadLine: # WICHTIG: AM Anfang keine ";" am Ende ein ";"- Array Fields2
31.12.2008 9:59:15 - ReadLine: #maila@domain.de;.txt;true;Folder1\;true;- Array Fields5
31.12.2008 9:59:15 - ReadLine: #maila@domain.de;*.txt,*.zip,*.asp;true;Folder2\;false;- Array Fields5
31.12.2008 9:59:15 - ReadLine: #mailb@domain.com;*.txt,*.zip;false;;true;- Array Fields5
31.12.2008 9:59:15 - ReadLine: info@domain.com;*.rar,*.xml,*.zip,*.asp;true;22\;true;- Array Fields5
31.12.2008 9:59:15 - Correct Line Found: info@domain.com;*.rar,*.xml,*.zip,*.asp;true;22\;true;
31.12.2008 9:59:15 - Email List was Readin
31.12.2008 9:59:15 - Attachment found

31.12.2008 9:59:15 - Adresse: info@domain.com
31.12.2008 9:59:15 - Extension: *.rar,*.xml,*.zip,*.asp
31.12.2008 9:59:15 - Logging: true
31.12.2008 9:59:15 - Directory: 22\
31.12.2008 9:59:15 - Overwrite File: true

31.12.2008 9:59:15 - Anzahl Empfanger1
31.12.2008 9:59:15 - Compare Empfanger:info@domain.com = info@domain.com== Ergebniss ==>1
xxxxx.rar
xxxxx.xml
xxxxx.zip
xxxxx.asp
31.12.2008 9:59:15 - Extensions vergleichen, Angaben - True
31.12.2008 9:59:15 - Vergleich von 80020_7070707070_20081229_5097.zip mit .rar
31.12.2008 9:59:15 - Vergleich von 80020_7070707070_20081229_5097.zip mit .xml
31.12.2008 9:59:15 - Vergleich von 80020_7070707070_20081229_5097.zip mit .zip      ----> asp as you can see there is no
31.12.2008 9:59:15 - Extension gefunden: .zip
31.12.2008 9:59:15 - File  C:\GetMail\22\80020_7070707070_20081229_5097.zip


Here's the script
EventHander.vbs
Code:
'   Sub OnClientConnect(oClient)
'   End Sub

'   Sub OnAcceptMessage(oClient, oMessage)
'   End Sub

'   Sub OnDeliveryStart(oMessage)
'   End Sub

'   Sub OnDeliverMessage(oMessage)
'   End Sub

'   Sub OnBackupFailed(sReason)
'   End Sub

'   Sub OnBackupCompleted()
'   End Sub

Option Explicit

'***********************************************
'* Script zur automatischen Speicherung von Attachments
'***********************************************

Sub OnAcceptMessage(oClient, oMessage)
   ' Script   
   Dim fs                        ' Fielsystem Objekt
   Dim cPath                     ' Der Pfad wo alles bespeichert wird
   Dim attachCount               ' Counter fur die Attachments
   Dim logger                    ' File in die das Logfile des Script geschrieben werden
   Dim dBug                      ' DBug Message ausgeben
   Dim DBugMeldung               ' Debug Meldungen
   Dim i,j,k,l                   ' Counter
   Dim msgFile                   ' Name der Email
   Dim logFile                   ' Name des Logfiles
   Dim doLogging                 ' Soll mit Geloggt werden
   Dim cMailDirectory            ' Directory in das die Attachments gespeichter werden
   Dim cConfiFile                ' File in dem beschrieben ist was bei welcher Email Adresse passiert
   Dim oFile                     ' FielsystemObjekt
   Dim cLine                     ' ConfigFile Zeile
   Dim aTemp                     ' Temporares Array
   Dim aExtensionArray           ' Array falls verschiedene Exstesnions betrachtet werden mussen               
   ReDim aConfigArray(4,0)       ' AktionenProEmail
   Dim bExtensionArray           ' Berucksichtigende Erweiterungen?
   Dim bExtensionFound           ' Extension Found
   Dim cTempFileName             ' File name falls er schon vorhanden ist
   
   cPath = "C:\GetMail\"
   cConfiFile = cPath & "list.csv"
   logFile = "logFileHmailServer.txt"
   dBug = true
   Set fs = CreateObject("Scripting.FileSystemObject")
   
   If dBug Then
      Set DBugMeldung = fs.OpenTextFile(cPath & "Debug.txt", 8, True)
      DBugMeldung.Writeline("--- New Message ---")
      DBugMeldung.Writeline(Now()& " - Script Started")
   End If
   
   ' Config File einlesen:
   If dBug Then
      DBugMeldung.Writeline(Now() & " - The Following Config File will be readin:" & cConfiFile)
   End If
   
   Set oFile = fs.OpenTextFile(cConfiFile, 1, False)
   i = 0
   Do While oFile.AtEndOfLine <> True
      cLine = oFile.ReadLine
      If dBug Then
         DBugMeldung.Writeline(Now() & " - ReadLine: " & cLine) & "- Array Fields" & ubound(Split(cLine,";"),1)
      End If
      If Mid(cLine, 1, 1) <> "#" and 5 = ubound(Split(cLine,";"),1) Then
         
          If dBug Then
           DBugMeldung.Writeline(Now() & " - Correct Line Found: " & cLine)
         End If
         
         
         ReDim Preserve aConfigArray(4, i + 1)
         
          aTemp = Split(cLine,";")
         'Empfanger Adresse
         aConfigArray(0,i) = aTemp(0)
         
         ' Attachment Extension
         aConfigArray(1,i) = aTemp(1)
         
         'Logging
         aConfigArray(2,i) = aTemp(2)
         
         'Directory
         aConfigArray(3,i) = aTemp(3)

         'Overwrite Existing File
         aConfigArray(4,i) = aTemp(4)
         
         i = i + 1

      End If
   loop
   Set oFile = Nothing

   If dBug Then
      DBugMeldung.Writeline(Now() & " - Email List was Readin")
   End If
   
   msgFile = oMessage.Filename
   ' Nur Ausfuhren Falls Attachment vorhanden sind
   
   If oMessage.Attachments.Count > 0 Then
     
      If dBug Then
        DBugMeldung.Writeline(Now() & " - Attachment found")
      End If
     
      'it has attachments, so we'll take action
      For j=0 to UBound(aConfigArray,2) - 1
         
         If dBug Then
            DBugMeldung.Writeline ""
            DBugMeldung.Writeline(Now() & " - Adresse: " & aConfigArray(0,j))
            DBugMeldung.Writeline(Now() & " - Extension: " & aConfigArray(1,j))
            DBugMeldung.Writeline(Now() & " - Logging: " & aConfigArray(2,j))
            DBugMeldung.Writeline(Now() & " - Directory: " & aConfigArray(3,j))   
            DBugMeldung.Writeline(Now() & " - Overwrite File: " & aConfigArray(4,j))   
            DBugMeldung.Writeline ""   
            DBugMeldung.Writeline(Now() & " - Anzahl Empfanger" &  oMessage.Recipients.Count)
         End If         
     
         For i = 0 To oMessage.Recipients.Count - 1
            If dBug Then
               DBugMeldung.Writeline(Now()& " - Compare Empfanger:" & aConfigArray(0,j) & " = " & oMessage.Recipients(i).Address & "== Ergebniss ==>" & InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ))
            End If

            If InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ) > 0 Then
           
               If LCase(aConfigArray(2,j)) = "false" Then
                  doLogging = false
               Else
                  doLogging = true
               End if
                             
               ' In welches Directory muss die Emial gespeichert werden
               cMailDirectory = aConfigArray(3,j)
               If Len(Trim( cMailDirectory )) <= 0 Then
                  cMailDirectory = Replace(Replace(Replace(aConfigArray(0,j),"@","_at_"),".","_")," ","_") & "\"
               End If             
               
               If not(fs.FolderExists(cPath & cMailDirectory)) Then
                    fs.CreateFolder(cPath & cMailDirectory)
               End If
               
               ' Muss der Empafng Protokolliert werden             
               If doLogging Then
                  Set logger = fs.OpenTextFile(cPath & cMailDirectory & logFile, 8, True)
                  logger.WriteLine("===== Log Entry For: " & msgFile & " - " & Now() & "=====")
                  logger.WriteLine("Recipients Matches: " & aConfigArray(0,j))
               End If           
       
               
               'Extension Array Aufbereiten
               If Len(aConfigArray(1,j)) > 1Then
                  If InStr(1,aConfigArray(1,j),",") = 0 Then
                     aConfigArray(1,j) = aConfigArray(1,j) & ","
                  End If
                  aExtensionArray = Split(aConfigArray(1,j),",")
                  For k = 0 to UBound(aExtensionArray,1)
                     aExtensionArray(k) = Replace(aExtensionArray(k),"*","")
                     DBugMeldung.Writeline "xxxxx" & aExtensionArray(k)
                  Next
                  bExtensionArray = true
                  If doLogging Then
                      logger.WriteLine("Extension Check: True")
                  End If
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions vergleichen, Angaben - " & bExtensionArray)
                  End If
               Else
                  bExtensionArray = false
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions nicht vergleichen, keine Angaben - " & bExtensionArray)
                  End If
               End If
               
               attachCount = oMessage.Attachments.Count
                           
               For l = 0 To attachCount - 1
                 
                  If bExtensionArray Then
                     bExtensionFound = false
                     For k = 0 to UBound(aExtensionArray,1)-1
                         
                         If dBug Then
                              DBugMeldung.Writeline(Now()& " - Vergleich von " & oMessage.Attachments(i).Filename & " mit " & aExtensionArray(k))
                        End If

                        If InStr(1,Right(oMessage.Attachments(i).Filename,Len(aExtensionArray(k))),aExtensionArray(k),1) > 0 Then
                            bExtensionFound = true
                            If doLogging Then
                               logger.WriteLine("Extension gefunden: " & aExtensionArray(k))
                            End If
                            If dBug Then
                              DBugMeldung.Writeline(Now()& " - Extension gefunden: " & aExtensionArray(k))
                           End If
                        End If
                     Next
                  End If
                       
                  If not(bExtensionArray) or bExtensionFound Then
                     cTempFileName = cPath & cMailDirectory & oMessage.Attachments(i).Filename
                     If LCase(aConfigArray(4,j)) = "true" Then
                        oMessage.Attachments(i).SaveAs cTempFileName
                     Else
                        Do While fs.FileExists(cTempFileName)
                           If doLogging Then
                               logger.WriteLine("File Exist: " & cTempFileName)
                            End If
                           cTempFileName = cPath & cMailDirectory & Replace(Replace(Replace(Now(),":","_"),".","_")," ","_") & "_" & oMessage.Attachments(i).Filename
                           If doLogging Then
                            logger.WriteLine("Try a new Filename: " & cTempFileName)
                         End If
                         If dBug Then
                            DBugMeldung.WriteLine("File Exist try a new Filename: " & cTempFileName)
                           End If
                        loop
                        oMessage.Attachments(i).SaveAs cTempFileName
                     End If
                     If doLogging Then
                        logger.WriteLine("*** Storing File --- filename: " & cTempFileName)
                        If dBug Then
                              DBugMeldung.Writeline(Now()& " - File  " & cTempFileName)
                        End If
                     End If
                  End If
               Next
               If doLogging Then
                  logger.Close()
                  Set logger = Nothing
               End If
            End If
         Next
      Next     
   End If
   
   If dBug Then
     DBugMeldung.Close()
     Set DBugMeldung = Nothing
   End If
   
   Set fs = Nothing
   
   ' set Result.Value = 0 so that the message is queued for delivery (according to hMailServer Docs
   Result.Value = 0
End Sub


Please help solve these problems
thanks in advance for the help and answers


all a Happy New Year


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2009-08-05 15:12 
New user
New user

Joined: 2009-08-05 14:57
Posts: 5
Hi

i´m new with this prog and i have some questions about that script here.

1. i place the code from here in the file "EventHandlers.vbs" under C:\Programme\hMailServer\Events
2. i create a " empfaengerliste.csv and but in same folder, create a log file also in the same folder "events"
3. change the path is the script for cPath to C:\Programme\hMailServer\Events
4. edit the empfaengerliste.csv that PPS and other file extensions should be save in a folder
5: restart services

is this correct? Or i have to put those files in another folder cause if i now send f.e. a PPS file attachments was
not save, mail goes through to the recipient without filtering the attachment.
What is my mistake?? Any help was appreciate.

thx and regards
ACID25


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2009-09-22 16:26 
New user
New user

Joined: 2009-08-05 14:57
Posts: 5
Hi

after holidays ;-) ...script is now working perfect...mistake was i had a blank line in the empfaengerliste.csv

but i have one question, the attachement was delivered to the recipient with the attachment....but i don´t want that, i want that these "bad"
attachments cut from the e-mail and safe in a folder? Is this possible? And how? My problem is i am not a developer and so scripting is more then difficult for me...


THX a lot!

ACID25


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2010-01-25 13:34 
New user
New user

Joined: 2009-05-15 10:19
Posts: 7
Very good script.

Thanks.


Top
 Profile  
 
 Post subject: Re: Script that saves attachment
PostPosted: 2010-08-05 16:28 
New user
New user

Joined: 2009-05-15 10:19
Posts: 7
I change this script. Attached files by year, month and day keeps the separated.

I've tested it works without problems.



Code:
'   Sub OnClientConnect(oClient)
'   End Sub

'   Sub OnAcceptMessage(oClient, oMessage)
'   End Sub

'   Sub OnDeliveryStart(oMessage)
'   End Sub

'   Sub OnDeliverMessage(oMessage)
'   End Sub

'   Sub OnBackupFailed(sReason)
'   End Sub

'   Sub OnBackupCompleted()
'   End Sub

Option Explicit

'***********************************************
'* Script zur automatischen Speicherung von Attachments
'***********************************************

Sub OnAcceptMessage(oClient, oMessage)
   ' Script   
   Dim fs                        ' Fielsystem Objekt
   Dim cPath                     ' Der Pfad wo alles bespeichert wird
   Dim attachCount               ' Counter fur die Attachments
   Dim logger                    ' File in die das Logfile des Script geschrieben werden
   Dim dBug                      ' DBug Message ausgeben
   Dim DBugMeldung               ' Debug Meldungen
   Dim i,j,k,l                   ' Counter
   Dim msgFile                   ' Name der Email
   Dim logFile                   ' Name des Logfiles
   Dim doLogging                 ' Soll mit Geloggt werden
   Dim cMailDirectory            ' Directory in das die Attachments gespeichter werden
   Dim cMailDirectoryd            ' Directory in das die Attachments gespeichter werden
   Dim cConfiFile                ' File in dem beschrieben ist was bei welcher Email Adresse passiert
   Dim oFile                     ' FielsystemObjekt
   Dim cLine                     ' ConfigFile Zeile
   Dim aTemp                     ' Temporares Array
   Dim aExtensionArray           ' Array falls verschiedene Exstesnions betrachtet werden mussen               
   ReDim aConfigArray(4,0)       ' AktionenProEmail
   Dim bExtensionArray           ' Berucksichtigende Erweiterungen?
   Dim bExtensionFound           ' Extension Found
   Dim cTempFileName             ' File name falls er schon vorhanden ist
   Dim tDeneme                   ' kjhkjh
   Dim tYil
   Dim tAy
   Dim tGun
   
   tYil = Year(Now()) & "\"
   tAy = Month(Now()) & "\"
   tGun = Day(Now()) & "\"
   
   tDeneme = Year(Now()) & "\" & Month(Now()) & "\" & Day(Now()) & "\"
   
   cPath = "C:\GetMail"
   cConfiFile = cPath & "list.csv"
   logFile = "logFileHmailServer.txt"
   dBug = true
   Set fs = CreateObject("Scripting.FileSystemObject")
   
   If dBug Then
      Set DBugMeldung = fs.OpenTextFile(cPath & "Debug.txt", 8, True)
      DBugMeldung.Writeline("--- New Message ---")
      DBugMeldung.Writeline(Now()& " - Script Started")
   End If
   
   ' Config File einlesen:
   If dBug Then
      DBugMeldung.Writeline(Now() & " - The Following Config File will be readin:" & cConfiFile)
   End If
   
  Set oFile = fs.OpenTextFile(cConfiFile, 1, False)
   i = 0
   Do While oFile.AtEndOfLine <> True
      cLine = oFile.ReadLine
      If dBug Then
         DBugMeldung.Writeline(Now() & " - ReadLine: " & cLine) & "- Array Fields" & ubound(Split(cLine,";"),1)
      End If
      If Mid(cLine, 1, 1) <> "#" and 5 = ubound(Split(cLine,";"),1) Then
         
          If dBug Then
           DBugMeldung.Writeline(Now() & " - Correct Line Found: " & cLine)
         End If
         
         
         ReDim Preserve aConfigArray(4, i + 1)
         
          aTemp = Split(cLine,";")
         'Empfanger Adresse
         aConfigArray(0,i) = aTemp(0)
         
         ' Attachment Extension
         aConfigArray(1,i) = aTemp(1)
         
         'Logging
         aConfigArray(2,i) = aTemp(2)
         
         'Directory
         aConfigArray(3,i) = aTemp(3)

         'Overwrite Existing File
         aConfigArray(4,i) = aTemp(4)
         
         i = i + 1

      End If
   loop
   Set oFile = Nothing

   If dBug Then
      DBugMeldung.Writeline(Now() & " - Email List was Readin")
   End If
   
   msgFile = oMessage.Filename
   ' Nur Ausfuhren Falls Attachment vorhanden sind
   
   If oMessage.Attachments.Count > 0 Then
     
      If dBug Then
        DBugMeldung.Writeline(Now() & " - Attachment found")
      End If
     
      'it has attachments, so we'll take action
      For j=0 to UBound(aConfigArray,2) - 1
         
         If dBug Then
            DBugMeldung.Writeline ""
            DBugMeldung.Writeline (Now() & tDeneme)
            DBugMeldung.Writeline(Now() & " - Adresse: " & aConfigArray(0,j))
            DBugMeldung.Writeline(Now() & " - Extension: " & aConfigArray(1,j))
            DBugMeldung.Writeline(Now() & " - Logging: " & aConfigArray(2,j))
            DBugMeldung.Writeline(Now() & " - Directory: " & aConfigArray(3,j))   
            DBugMeldung.Writeline(Now() & " - Overwrite File: " & aConfigArray(4,j))   
            DBugMeldung.Writeline ""   
            DBugMeldung.Writeline(Now() & " - Anzahl Empfanger" &  oMessage.Recipients.Count)
         End If         
     
         For i = 0 To oMessage.Recipients.Count - 1
            If dBug Then
               DBugMeldung.Writeline(Now()& " - Compare Empfanger:" & aConfigArray(0,j) & " = " & oMessage.Recipients(i).Address & "== Ergebniss ==>" & InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ))
            End If

            If InStr(1,aConfigArray(0,j),oMessage.Recipients(i).Address, 1 ) > 0 Then
           
               If LCase(aConfigArray(2,j)) = "false" Then
                  doLogging = false
               Else
                  doLogging = true
               End if
                             
               ' In welches Directory muss die Emial gespeichert werden
               cMailDirectory = aConfigArray(3,j)
               
               If Len(Trim( cMailDirectory )) <= 0 Then
                  cMailDirectory = Replace(Replace(Replace(aConfigArray(0,j),"@","_at_"),".","_")," ","_") & "\"
               End If               
               If not(fs.FolderExists(cPath & cMailDirectory)) Then                   
                    fs.CreateFolder(cPath & cMailDirectory)
               End If
               If Not(fs.FolderExists(cPath & cMailDirectory & tYil)) Then             
                    fs.CreateFolder(cPath & cMailDirectory & tYil)
               End If
               If Not(fs.FolderExists(cPath & cMailDirectory & tYil & tAy)) Then
                    fs.CreateFolder(cPath & cMailDirectory & tYil & tAy)
               End If
               If Not(fs.FolderExists(cPath & cMailDirectory & tYil & tAy & tGun)) Then
                    fs.CreateFolder(cPath & cMailDirectory & tYil & tAy & tGun)
               End If
               
               ' Muss der Empafng Protokolliert werden
               If doLogging Then
                  Set logger = fs.OpenTextFile(cPath & cMailDirectory & logFile, 8, True)
                  logger.WriteLine("===== Log Entry For: " & msgFile & " - " & Now() & "=====")
                  logger.WriteLine("Recipients Matches: " & aConfigArray(0,j))
               End If           
       
               
               'Extension Array Aufbereiten
               If Len(aConfigArray(1,j)) > 1Then
                  If InStr(1,aConfigArray(1,j),",") = 0 Then
                     aConfigArray(1,j) = aConfigArray(1,j) & ","
                  End If
                  aExtensionArray = Split(aConfigArray(1,j),",")
                  For k = 0 to UBound(aExtensionArray,1)
                     aExtensionArray(k) = Replace(aExtensionArray(k),"*","")
                     DBugMeldung.Writeline "xxxxx" & aExtensionArray(k)
                  Next
                  bExtensionArray = true
                  If doLogging Then
                      logger.WriteLine("Extension Check: True")
                  End If
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions vergleichen, Angaben - " & bExtensionArray)
                  End If
               Else
                  bExtensionArray = false
                  If dBug Then
                     DBugMeldung.Writeline(Now()& " - Extensions nicht vergleichen, keine Angaben - " & bExtensionArray)
                  End If
               End If
               
               attachCount = oMessage.Attachments.Count
                           
               For l = 0 To attachCount - 1
                 
                  If bExtensionArray Then
                     bExtensionFound = false
                     For k = 0 to UBound(aExtensionArray,1)-1
                         
                         If dBug Then
                              DBugMeldung.Writeline(Now()& " - Vergleich von " & oMessage.Attachments(i).Filename & " mit " & aExtensionArray(k))
                        End If

                        If InStr(1,Right(oMessage.Attachments(i).Filename,Len(aExtensionArray(k))),aExtensionArray(k),1) > 0 Then
                            bExtensionFound = true
                            If doLogging Then
                               logger.WriteLine("Extension gefunden: " & aExtensionArray(k))
                            End If
                            If dBug Then
                              DBugMeldung.Writeline(Now()& " - Extension gefunden: " & aExtensionArray(k))
                           End If
                        End If
                     Next
                  End If
                       
                  If not(bExtensionArray) or bExtensionFound Then
                     cTempFileName = cPath & cMailDirectory & tDeneme & oMessage.Attachments(i).Filename
                     If LCase(aConfigArray(4,j)) = "true" Then
                        oMessage.Attachments(i).SaveAs cTempFileName
                     Else
                        Do While fs.FileExists(cTempFileName)
                           If doLogging Then
                               logger.WriteLine("File Exist: " & cTempFileName)
                            End If
                           cTempFileName = cPath & cMailDirectory & tDeneme & Replace(Replace(Replace(Now(),":","_"),".","_")," ","_") & "_" & oMessage.Attachments(i).Filename
                           If doLogging Then
                            logger.WriteLine("Try a new Filename: " & cTempFileName)
                         End If
                         If dBug Then
                            DBugMeldung.WriteLine("File Exist try a new Filename: " & cTempFileName)
                           End If
                        loop
                        oMessage.Attachments(i).SaveAs cTempFileName
                     End If
                     If doLogging Then
                        logger.WriteLine("*** Storing File --- filename: " & cTempFileName)
                        If dBug Then
                              DBugMeldung.Writeline(Now()& " - File  " & cTempFileName)
                        End If
                     End If
                  End If
               Next
               If doLogging Then
                  logger.Close()
                  Set logger = Nothing
               End If
            End If
         Next
      Next     
   End If
   
   If dBug Then
     DBugMeldung.Close()
     Set DBugMeldung = Nothing
   End If
   
   Set fs = Nothing
   
   ' set Result.Value = 0 so that the message is queued for delivery (according to hMailServer Docs
   Result.Value = 0
End Sub


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest



Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group