how to delete unwanted attachments for a specific domain

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
Post Reply
rodolfor
Senior user
Senior user
Posts: 282
Joined: 2005-06-30 09:05
Location: Gubbio - Italy

how to delete unwanted attachments for a specific domain

Post by rodolfor » 2006-02-11 15:53

This script delete unwanted attachments for incoming messages only for the domain TEST.COM and inform the receiver about files deleted.

Code: Select all

const gc_path_hmail = "D:\hMailServer\"
const gc_sDQ	= """" 
const chf_sName = "KERB"
const chf_sFileTest = "|.PDF|.JPG|.TIF|.GIF|.BMP|.PNG|"    'extension admitted

'*************************************************************************************************************
Sub OnDeliverMessage(oMessage)

	Dim chf_sPath 
	Dim chf_sLogFile 	

	chf_sPath = gc_path_hmail & "Events\"
	chf_sLogFile = gc_path_hmail & "Logs\checkfile.log"

	if oMessage.Attachments.Count>0 then
	
		if ExistsInRecipients(oMessage,"@test.com") then
	 
			WriteLog chf_sLogFile, oMessage.From & " to " & oMessage.Recipients(0).Address
			
			dim i 
			dim moda 
			dim obAttachment
			dim sBody
		 
			sBody = ""
			moda = false
			
			for i = 0 to oMessage.Attachments.Count - 1
				
				set obAttachment = oMessage.Attachments(i)
			
				nf = obAttachment.FileName				
				ef = ucase(right(nf,4))
		   
				if (InStr(chf_sFileTest, ef) = 0) then
					'delete the attachment
					obAttachment.Delete
					WriteLog  chf_sLogFile, "           " & "deleted attachment '" & nf & "'"
					sBody = sBody & "[" & chf_sName & "] deleted attachment '" & nf & "'<br>"
					moda = true
				end if
		   
			next
			
			'set oCheck = nothing
			
			if (moda = true) then 
				oMessage.Subject = "[" & chf_sName  & "] - " & oMessage.Subject
				oMessage.Body = sBody & oMessage.Body 
				oMessage.Save()
			end if
		
		End If
		
	End If

End Sub	


'*************************************************************************************************************
sub WriteLog(sFile, sText)

 dim oFil, oFs
 
 SET oFs = CreateObject("Scripting.FileSystemObject")
 SET oFil =  ofs.OpenTextFile(sFile, 8, True) 
 
 oFil.WriteLine(Now & "   " & sText)  
 oFil.Close
 
 set oFil = nothing
 set oFs = nothing
 
end sub

 
'*************************************************************************************************************
' TRUE if sDEST string exists as part of one of recipients
Function ExistsInRecipients(oMessage, sDest) 

	dim i
	dim temp 
	
	temp = false	
	sDest = ucase(sDest)
	for i= 0 to oMessage.Recipients.Count-1
		if ucase(right(oMessage.Recipients(i).Address,len(sDest)))=sDest then
			temp = true
			exit for
		end if
	next
	
	ExistsInRecipients = temp
	
end Function

User avatar
kani
New user
New user
Posts: 18
Joined: 2010-04-01 18:31

Re: how to delete unwanted attachments for a specific domain

Post by kani » 2010-04-08 22:13

This helped me a lot!

Thank you very much!!! :D

bigworm
Normal user
Normal user
Posts: 45
Joined: 2007-11-14 22:13

Re: how to delete unwanted attachments for a specific domain

Post by bigworm » 2010-09-15 18:20

wonder if this works on v5?
hMailServer 5.4 - Build 1946 + ASSP v2.4.3(14246)

rodolfor
Senior user
Senior user
Posts: 282
Joined: 2005-06-30 09:05
Location: Gubbio - Italy

Re: how to delete unwanted attachments for a specific domain

Post by rodolfor » 2010-09-15 20:41

yes
Hmailserver [lastversion] + MSSQL

sss123
New user
New user
Posts: 12
Joined: 2010-11-15 17:49
Location: UK
Contact:

Re: how to delete unwanted attachments for a specific domain

Post by sss123 » 2011-08-19 17:31

Hello all,

How can implement this script? I tried pasting it into the EventHandlers file and then reloading it in Hmail admin and changed the Test.com to my domain name but it doesn't seem to be working. I am new to scripting and vb etc, so if someone could point me in the right direction I'd be very grateful.

Thanks in advance, Mike

Post Reply