Is there a script for hmailServer to extract attachments from incoming emails?

2.1k Views Asked by At

I have emails coming into my hmailserver. I need to write rules/scripts that take the emails (all have attachments), and extract the file they each contain.

Bonus points if you can help me sort these extracted files into specific locations on my D:/ drive!

2

There are 2 best solutions below

0
On

To possible ways, first: use IMAP with a language of your choice, to connect to the wanted account then search for imap body parse code to extract attachment to desired location. Second possiblity is to use server side script in vbscript in EventHandler file your work is to implement this method

        Sub OnDeliverMessage(oMessage)
Enter code Here

    '   End Sub
0
On

You could define a function SaveAttachments and call from the OnDeliverMessage event

    Sub OnDeliverMessage(oMessage)
       call SaveAttachments(oMessage)
    End Sub

''''''''''''''''

    Sub SaveAttachments(oMessage)
    ' this routine saves file attachments that have the specified
    ' FileExtensions. 


      Dim SaveFolder   'where to store attachments. This folder must already exist. 
      SaveFolder = "c:\path_to_folder\"     ' trailing slash is required

      Dim SavedFile

      Dim FileExtensions ' set to the file attachment extensions you want to save
      FileExtensions = "(pdf|doc)"  

      Dim oAttachment    
      Dim oRegExp 
      Set oRegExp = new RegExp    
      For oAttachment = 0 to oMessage.Attachments.Count-1
      ' Test for specified attachments
       with oRegExp
        .Pattern = "^.*\."& FileExtensions & "$" 
        .IgnoreCase = True
        .Global = False
       end with
       if (oRegExp.test(oMessage.Attachments(oAttachment).Filename)) Then

         SavedFile = SaveFolder & Left(Right(oMessage.Filename,42),38) & "." & oAttachment & "." & oMessage.Attachments(oAttachment).Filename
         oMessage.Attachments(oAttachment).SaveAs(SavedFile)
         ' uncomment below to Delete Attachment from message
         'oMessage.Attachments(oAttachment).Delete
       End If 
      Next
      Set oRegExp = nothing

    End Sub