Exclude attachment blocking for a domain name

Use this forum if you have problems with a hMailServer script, such as hMailServer WebAdmin or code in an event handler.
Post Reply
Mrns
New user
New user
Posts: 18
Joined: 2012-02-24 17:10

Exclude attachment blocking for a domain name

Post by Mrns » 2020-09-04 17:25

How can I bypass attachment blocking for a domain name.
I have blocked .doc extensions etc .. following the spread of a virus.
However, some more experienced users are complaining that they must receive doc files.

I cannot create ranges of ip for each contact of my clients.

there is a way to allow only a certain domain name to receive these files?

User avatar
mattg
Moderator
Moderator
Posts: 21268
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Exclude attachment blocking for a domain name

Post by mattg » 2020-09-05 03:25

You would need to script the removal of .doc files and stop mail for your domain using that function
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Mrns
New user
New user
Posts: 18
Joined: 2012-02-24 17:10

Re: Exclude attachment blocking for a domain name

Post by Mrns » 2020-09-05 12:02

thanks you put me on the right path
I tried to use this script
https://www.hmailserver.com/forum/viewt ... 75#p165892
The deletion works fine, but I can't get the notification. Can someone help me?

Code: Select all

Sub DeleteAttachments(oMessage)
' this routine deletes or quarantines file attachments that have the specified
' FileExtensions. It replaces the functionality of antivirus blocked attachments panel in hmailadmin
' and also has a selectable quarantine option

  Dim Quarantine  ' set to True to Quarantine or False to Delete attachments
  Quarantine = False

  Dim QuarantineFolder   'where to store quarantined attachments. This folder must already exist. 
  QuarantineFolder = "c:\path to quarantine folder\"     ' trailing slash is required
  
  Dim QuarantineFile

  Dim FileExtensions ' set to the file attachment extensions you want to delete or quarantine
  FileExtensions = "(bat|cmd|com|cpl|csh|docm|exe|pif|hta|htb|inf|js|jse|lnk|msi|msp|pif|reg|scf|scr|shs|shb|vbe|vbs|wsf|wsh)"  
   
 ' change anything below here at your own risk. 

  Dim AttachmentDeleteNotify
  AttachmentDeleteNotify = Empty
  Dim oAttachment    
  Dim oRegExp 
  Set oRegExp = new RegExp    
  For oAttachment = 0 to oMessage.Attachments.Count-1
  ' Test for undesirable attachments
   with oRegExp
    .Pattern = "^.*\."& FileExtensions & "$" 
    .IgnoreCase = True
    .Global = False
   end with
   if (oRegExp.test(oMessage.Attachments(oAttachment).Filename)) Then
    ' Delete Attachment
    AttachmentDeleteNotify = AttachmentDeleteNotify & oMessage.Attachments(oAttachment).Filename & "~"
    If (Quarantine) Then
     QuarantineFile = QuarantineFolder & Left(Right(oMessage.Filename,42),38) _
      & "." & oAttachment & "." & oMessage.Attachments(oAttachment).Filename & "." & ".Quarantined"
     oMessage.Attachments(oAttachment).SaveAs(QuarantineFile)
     oMessage.Attachments(oAttachment).Delete
    Else
     oMessage.Attachments(oAttachment).Delete
    End If 
   End If 
  Next
  Set oRegExp = nothing
  If Not IsEmpty(AttachmentDeleteNotify) then
   AttachmentDeleteNotify = "The following attachment/s were blocked for delivery by the e-mail server." & "~" _
   & "Please contact your system administrator if you have any questions regarding this." & "~" & "~" _
   & "Notice ID: " & Left(Right(oMessage.Filename,42),38) & "~" & "~" _
   & AttachmentDeleteNotify _
   & "~" & "Mail Server Admin" & "~" & "~"
   Dim htmlNotify
   htmlNotify = AttachmentDeleteNotify
   If ( Len(oMessage.Body) <> 0 ) Then
    AttachmentDeleteNotify = Replace(AttachmentDeleteNotify, "~", VbCrLf)   
    oMessage.Body = oMessage.Body & VbCrLf & VbCrLf & AttachmentDeleteNotify
   End If 
   If ( Len(oMessage.HTMLBody) <> 0 ) Then
    htmlNotify = Replace(htmlNotify, "~", ("<br>" & VbCrLf))
    oMessage.HTMLBody = Replace(oMessage.HTMLBody, "</body", ("<br><br>" & htmlNotify & "</BODY"), 1, 1, vbTextCompare)
   End If 
   oMessage.Save
  End If 
End Sub

Mrns
New user
New user
Posts: 18
Joined: 2012-02-24 17:10

Re: Exclude attachment blocking for a domain name

Post by Mrns » 2020-09-23 11:39

Please someone who can help me I can't send the notification, it's Important

palinka
Senior user
Senior user
Posts: 2475
Joined: 2017-09-12 17:57

Re: Exclude attachment blocking for a domain name

Post by palinka » 2020-09-23 13:33

Are there any errors in your error log?

Did you look in your hmailserver log for any clues?

How are you calling the script?

Mrns
New user
New user
Posts: 18
Joined: 2012-02-24 17:10

Re: Exclude attachment blocking for a domain name

Post by Mrns » 2020-09-29 12:20

Hello
the function is placed in the EventHandlers.vbs file and is called by a rule.
I don't see anything out of the ordinary in the mail server log
Nothing appears in the error log.

The script should add notification that the attachment has been removed to the message. The attachment is successfully removed but the notification is not inserted
rem1.jpg

Mrns
New user
New user
Posts: 18
Joined: 2012-02-24 17:10

Re: Exclude attachment blocking for a domain name

Post by Mrns » 2020-10-02 09:51

This code allows creating a rule that calls the DeleteAttachments function on the account or general
block attachments only for a specific account or domain

This is the working version.


Code: Select all

Sub DeleteAttachments(oMessage)
' this routine deletes or quarantines file attachments that have the specified
' FileExtensions. It replaces the functionality of antivirus blocked attachments panel in hmailadmin
' and also has a selectable quarantine option

  Dim Quarantine  ' set to True to Quarantine or False to Delete attachments
  Quarantine = False

  Dim QuarantineFolder    'where to store quarantined attachments. This folder must already exist.
  QuarantineFolder = "c:\quarantine\"     ' a slash bar is required
  Dim QuarantineFile

  Dim FileExtensions ' set to the file attachment extensions you want to delete or quarantine
  FileExtensions = "(doc|docx|bat|cmd|com|cpl|csh|docm|exe|pif|hta|htb|inf|js|jse|lnk|msi|msp|pif|reg|scf|scr|shs|shb|vbe|vbs|wsf|wsh)"  
   
 ' change anything below here at your own risk. 

  Dim AttachmentDeleteNotify
  AttachmentDeleteNotify = Empty
Dim oAttachment    
  Dim oRegExp 
  Set oRegExp = new RegExp    
  
  For oAttachment = 0 to oMessage.Attachments.Count-1
  ' Verifica la presenza di allegati indesiderati
   with oRegExp
    .Pattern = "^.*\."& FileExtensions & "$" 
    .IgnoreCase = True
    .Global = False
   end with
   
  if (oRegExp.test(oMessage.Attachments(oAttachment).Filename)) Then
    ' Elimina Allegati
    AttachmentDeleteNotify = AttachmentDeleteNotify & oMessage.Attachments(oAttachment).Filename & " ;"
    If (Quarantine) Then
     QuarantineFile = QuarantineFolder & Left(Right(oMessage.Filename,42),38) _
      & "." & oAttachment & "." & oMessage.Attachments(oAttachment).Filename & "." & ".Quarantined"
     oMessage.Attachments(oAttachment).SaveAs(QuarantineFile)
     oMessage.Attachments(oAttachment).Delete
    Else
     oMessage.Attachments(oAttachment).Delete
    End If 
		
   End If 
  Next
    
  ' Adds a notification to the message
  Set oRegExp = nothing
  If Not IsEmpty(AttachmentDeleteNotify) then
    AttachmentDeleteNotify = "------"&vbcrlf&"Alert:"&vbcrlf&"Some attachments deemed potentially dangerous have been deleted from this email."&vbcrlf&"If you are sure that the file is safe, you can ask the sender to compress it into a ZIP file. Then send it as an attachment."& vbcrlf & "Notice ID: " & Left(Right(oMessage.Filename,42),38) & vbcrlf &"Deleted Attachments: "& AttachmentDeleteNotify &vbcrlf&"Other files may have been deleted by the antivirus and notified in a text file attached to this message"& vbcrlf & "Admin Mail Server"& vbcrlf&"------"
    
	oMessage.Body=oMessage.Body & vbcrlf & AttachmentDeleteNotify
		
   if oMessage.HTMLBody <> "" then
		AttachmentDeleteNotify=replace(AttachmentDeleteNotify,vbcrlf,"<br>")   
		oMessage.HTMLBody = oMessage.HTMLBody & "<br>" &AttachmentDeleteNotify
   end if
   
   oMessage.Save
       
  End If 
End Sub

palinka
Senior user
Senior user
Posts: 2475
Joined: 2017-09-12 17:57

Re: Exclude attachment blocking for a domain name

Post by palinka » 2020-10-02 15:36

👍 Nice.

Post Reply