My script to delete spam email after x days old, with report

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
gsmaclean
Normal user
Normal user
Posts: 47
Joined: 2008-04-16 18:35

My script to delete spam email after x days old, with report

Post by gsmaclean » 2008-05-30 21:46

I mentioned this script that I had written the other day, and someone asked me for it. Here is what it does:
  • Scans every user in every domain
  • Looks for an IMAP folder named "Spam" for each user. If one doesn't already exist, it creates one
  • Scans through each "Spam" IMAP folder. Any messages in that folder that are older than a given number of days (defaults to 7, can be changed at the top of the script) are deleted
  • When finished, generates a report and emails it to an administrative address. The report contains a line for each domain showing:

    - Number of mail accounts in the domain
    - Total size of all messages stored for that domain
    - Number of messages stored for that domain
    - Number of spam messages stored for that domain
    - Number of spam messages that were just deleted for that domain

    At the end of the report it shows a grand total for all domains on the server.
I set this up to run once a day at midnight on my server.

To get the spam into the Spam folders in the first place, I set up a global rule. In my case, I use ASSP as a spam filter, and I have set it up to mark messages it considers to be spam with this in the message header:

X-Assp-Spam=YES

The global filter looks for this field, and if it finds it, it moves the message to the "Spam" IMAP folder. This moves all spam for all users into their appropriate Spam folder. There they have a week to review their spam folders to make sure a false positive didn't tag a valid message. They are free to delete the messages from their spam folder, or after 7 days, it is done for them.

Code: Select all

' DelOldSpam.vbs - Scott MacLean - v1.0 - 05/30/2008

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam

AdminUser = "Administrator" ' Replace this with your administrator username
AdminPass = "YourPassword"  ' Replace this with your administrator password
FromEmail = "from@address.com" ' Replace this with the email address you want the report to come "from"
ReportToEmail = "report@address.com" ' Replace this with the email address you want the report to be sent to
DaysOfSpam = 7 ' Replace this with the number of days spam is kept before being deleted

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim Message

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum

Dim FoundSpamFolder

Dim OutputMsg

Dim NumMsgs
Dim AccountSize
Dim NumSpams
Dim NumDeleted
Dim TotalAccounts
Dim TotalNumMsgs
Dim TotalAccountSize
Dim TotalNumSpams
Dim TotalNumDeleted

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

TotalNumMsgs = 0
TotalAccountSize = 0
TotalNumSpams = 0
TotalNumDeleted = 0

Set Domains = App.Domains

For DomainNum = 1 To Domains.Count
  Set Domain = Domains.Item(DomainNum - 1)
  
  If Domain.Active Then
  
    Set Accounts = Domain.Accounts
    
    NumMsgs = 0
    AccountSize = 0
    NumSpams = 0
    NumDeleted = 0
    
    For AccountNum = 1 To Accounts.Count
      Set Account = Accounts.Item(AccountNum - 1)
  
      If Account.Active Then
      
        Set IMAPFolders = Account.IMAPFolders
        
        FoundSpamFolder = False
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
          If UCase(IMAPFolder.Name) = "SPAM" Then 
            FoundSpamFolder = True
            Exit For
          End If
        Next
        
        If Not FoundSpamFolder Then Set IMAPFolder = IMAPFolders.Add("Spam")
        
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)

          Set Messages = IMAPFolder.Messages

          If UCase(IMAPFolder.Name) = "SPAM" Then 
            NumSpams = NumSpams + Messages.Count
            TotalNumSpams = TotalNumSpams + Messages.Count
          End If

          NumMsgs = NumMsgs + Messages.Count
          TotalNumMsgs = TotalNumMsgs + Messages.Count
          
          For MessageNum = Messages.Count To 1 Step -1
            Set Message = Messages.Item(MessageNum - 1)

            AccountSize = AccountSize + Message.Size
            TotalAccountSize = TotalAccountSize + Message.Size
            
            If UCase(IMAPFolder.Name) = "SPAM" Then 

              If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
                NumDeleted = NumDeleted + 1
                TotalNumDeleted = TotalNumDeleted + 1
                
                Messages.DeleteByDBID Message.ID
              End If
            End If
          Next
        Next          
      End If
    Next
    
    TotalAccounts = TotalAccounts + Accounts.Count
    
    OutputMsg = OutputMsg & "<tr><td>" & Domain.Name & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(Accounts.Count, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AccountSize, 0, True, False, True) & "K</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumSpams, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf
    
  End If
Next

OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td><b>Users</b></td><td><b>Size</b></td><td><b>Messages</b></td><td><b>Spam</b></td><td><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg

OutputMsg = OutputMsg & "<tr><td><b>Total</b></td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccounts, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccountSize, 0, True, False, True) & "K</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumMsgs, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumSpams, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf

OutputMsg = OutputMsg & "</table>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.FromAddress = FromEmail
Message.From = "Spam Deletion Daemon"
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily spam deletion report"
Message.HTMLBody = OutputMsg
Message.Save

ewieldra
Normal user
Normal user
Posts: 37
Joined: 2008-04-24 23:23
Location: The Netherlands

Re: My script to delete spam email after x days old, with report

Post by ewieldra » 2008-06-11 00:12

@gsmaclean

Yet another NICE script from you .. just like the listserver..

maybe another idea.. because it seems so easy to you

Creating a status message of the spam mails in the spam folder and send it to a specified email address.

In the message

From - To - Subject - Date

This will make it a lot easier for the owner of the spambox to see which mail is in the spamfolder without opening the mailbox
Best regards,

Emiel Wieldraaijer
The Netherlands

gsmaclean
Normal user
Normal user
Posts: 47
Joined: 2008-04-16 18:35

Re: My script to delete spam email after x days old, with report

Post by gsmaclean » 2008-07-11 21:40

I've added to the script above to do something very similar to what you asked.

In the script you'll see one additional line at the top:

Code: Select all

SendSpamReport = True ' If true, each user gets an email listing all emails in their spam folder, if false, no email is sent
If this is set to False, the script operates exactly as it did previously.

If it is set to true, then every time the script is run, it scans through every mailbox on the server. After deleting messages from the "Spam" folder of each mailbox, those mailboxes that still have one or more messages remaining in their Spam folders are sent an email listing the From, Date and Subject of each of those messages.
Attachments
DelOldSpam.zip
Delete Old Spam version 1.1
(1.71 KiB) Downloaded 826 times

ewieldra
Normal user
Normal user
Posts: 37
Joined: 2008-04-24 23:23
Location: The Netherlands

Re: My script to delete spam email after x days old, with report

Post by ewieldra » 2008-07-13 14:25

@gsmaclean

Excellent... :D

Not 100% what i wanted .. but this will do the trick also ..

thank you very much ...
Best regards,

Emiel Wieldraaijer
The Netherlands

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2008-07-22 16:19

gsmaclean: I found your script and have been playing with it to work on a single account (a catch-all) to delete every e-mail in the account over 2 days old. While I was doing that, I found what may be a bug in your script and thought I'd point it out and suggest a fix.
When I run my version to delete the e-mails, I found that it would only delete every other e-mail (ie: if there were 5 e-mails in the account, it would delete 2 of them)
What seemed to be happening was as the script was deleting them, the indexes seemed to be moving down (ie: delete message #0, message #1 -> message#0, etc)

The fix was that i would delete message(0), let the for increment, and then delete message(0) again (the former message(1))

basically

Code: Select all

NumMsgs = NumMsgs + Messages.Count
For MessageNum = 1 To Messages.Count
'    Set Message = Messages.Item(MessageNum-1)
    Set Message = Messages.Item(0)
    AcountSize = AccountSize + Message.Size
    Messages.DeleteByDBID Message.ID
Next

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2008-07-22 18:09

Ok, I was wrong.
The problem only happens when you go from the first item to the last, which is what happened on my modified script. Going from the last to the first (step -1) as you delete a file, the number moves with it, so my apologies.

gsmaclean
Normal user
Normal user
Posts: 47
Joined: 2008-04-16 18:35

Re: My script to delete spam email after x days old, with report

Post by gsmaclean » 2008-07-25 22:14

pmw wrote:Ok, I was wrong.
The problem only happens when you go from the first item to the last, which is what happened on my modified script. Going from the last to the first (step -1) as you delete a file, the number moves with it, so my apologies.
Right, that is why you see the Step - 1 on this line of my code:

Code: Select all

For MessageNum = Messages.Count To 1 Step -1

bigcrawdaddy
Normal user
Normal user
Posts: 53
Joined: 2008-02-19 14:41

Re: My script to delete spam email after x days old, with report

Post by bigcrawdaddy » 2008-09-05 16:22

How can I modify this script in order to delete messages from spam folder that are dated with future dates. Like today is Sept 05, 2008 and there are some in my spam folder with dates of Oct 04, 2008

So what if anything can I add to this script to deal with these? Or, do I need to deal with them in a different way?

Thanks

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2008-09-05 17:15

bigcrawdaddy:
I'm just glancing at the copy I have here, and I'd say the line that reads

Code: Select all

If DateAdd("d",DaysOfSpam, Message.InternalDate)<Now Then
would be the line to modify.

Suggested to:

Code: Select all

If DateAdd("d",0,Message.InternalDate)>Now Then
(there's probably a better way on this)

However, I'm not sure if Message.InternalDate is the system date stamped when the message arrived (in which case this will not work, because it will never be in the future) or the date that shows up in the e-mail. I'm thinking it's the latter.

bigcrawdaddy
Normal user
Normal user
Posts: 53
Joined: 2008-02-19 14:41

Re: My script to delete spam email after x days old, with report

Post by bigcrawdaddy » 2008-09-05 20:38

I have tried that and your correct it will not work.

Thinking maybe I will need some sort of rule or another script to stop messages that have future dates.

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2008-09-05 22:17

Honestly, I'd just go with the original delete spam script from gsmaclean. The messages that have future dates will delete after a week (or however many days you choose). Or do some poking on the hMail API (don't ask me where it is, I can't remember and don't have an hour to try and hunt it down) and see if there's a way to check the date that's listed in your client. I'm sure someone would be happy to help you on that project. I'd probably be willing to crack at it a little, but I wouldn't promise nothing.

bigcrawdaddy
Normal user
Normal user
Posts: 53
Joined: 2008-02-19 14:41

Re: My script to delete spam email after x days old, with report

Post by bigcrawdaddy » 2008-09-08 19:05

Found some code from another posting in the forum and edited the script to do what I was wanting done. Have attached it below.
Attachments
CDDefOldSpam.rar
(1.92 KiB) Downloaded 693 times

User avatar
simondsmason
Normal user
Normal user
Posts: 147
Joined: 2007-11-29 00:52

Re: My script to delete spam email after x days old, with report

Post by simondsmason » 2008-10-11 14:56

I would like to implement the script and I have a couple of newbie questions (I checked all docs and searched forum before posting these basic questions!).

1. I have one script in eventhandler.vbs currently under OnDeliverMessage - my spam checking - where do I put this script and how do I trigger it once a night?

2. Can you post the code you use to move the spam messages into the sub-folder in the first place?

Thanks.
Simon DS Mason

ewieldra
Normal user
Normal user
Posts: 37
Joined: 2008-04-24 23:23
Location: The Netherlands

Re: My script to delete spam email after x days old, with report

Post by ewieldra » 2008-10-11 15:45

HI simondsmason,

it simple

save the vbs file to a location and add schedule a task to execute the file every day
Best regards,

Emiel Wieldraaijer
The Netherlands

User avatar
simondsmason
Normal user
Normal user
Posts: 147
Joined: 2007-11-29 00:52

Re: My script to delete spam email after x days old, with report

Post by simondsmason » 2008-10-11 16:08

Please disregard my previous question about moving SPAM - I figured that out.

How do I schedule a vbs file to run - are we talking about Windows scheduler or from within hMail? Thanks.
Simon DS Mason

ewieldra
Normal user
Normal user
Posts: 37
Joined: 2008-04-24 23:23
Location: The Netherlands

Re: My script to delete spam email after x days old, with report

Post by ewieldra » 2008-10-11 20:08

Windows Task scheduler
Best regards,

Emiel Wieldraaijer
The Netherlands

User avatar
simondsmason
Normal user
Normal user
Posts: 147
Joined: 2007-11-29 00:52

Re: My script to delete spam email after x days old, with report

Post by simondsmason » 2008-10-12 03:15

Thanks
Simon DS Mason

ThomasJohansen
Normal user
Normal user
Posts: 40
Joined: 2007-07-11 09:54
Location: Denmark

Re: My script to delete spam email after x days old, with report

Post by ThomasJohansen » 2008-11-20 09:38

Hi


Looks like a nice script.


I could use something like this but I have already collected all the spam messages and moved them to a single account only for this purpose.

If anyone have a slight modification of this script, where it only deletes all messages older than X days in a specific account weither or not its spam, and wants to share it. I would really appreciate it, since my script capabilities arent that good.


Thx in advange

Thomas

aty154
New user
New user
Posts: 9
Joined: 2006-05-25 14:45
Location: Pakistan/Karachi
Contact:

Re: My script to delete spam email after x days old, with report

Post by aty154 » 2009-02-01 21:35

I try this script on my one of domain with around 20K messages in spam folder. Script send email telling me no of messages and users of domain but not able to delete any spam email and old email. I don't understand what might be wrong?

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-02-01 23:24

What version of hMail are you running it on? Are any errors reported in the error log or standard logs? you know the usual useful information!!
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

ed82264
New user
New user
Posts: 10
Joined: 2009-02-01 04:59
Location: Ma
Contact:

Re: My script to delete spam email after x days old, with report

Post by ed82264 » 2009-02-04 02:11

Is it normal that this script sends 168 emails once implemented?

once I started this script, and opened my email, I received 168 emails with the number of spam emails I received for the past 19 hours.

Another question on this script, though; is it possible to add lines to the script listing the accounts and the total amount of spam received under each account?

Just curious on that, and looking for an easier solution than ME trying to figure it out; i've been away from script for quite a while, and would probably get myself in more trouble than what it's worth.

Thanks

Ed

ed82264
New user
New user
Posts: 10
Joined: 2009-02-01 04:59
Location: Ma
Contact:

Re: My script to delete spam email after x days old, with report

Post by ed82264 » 2009-02-04 03:06

gsmaclean
ed82264 wrote:Is it normal that this script sends 168 emails once implemented?

once I started this script, and opened my email, I received 168 emails with the number of spam emails I received for the past 19 hours.
Let me re-phrase that; the emails I received were the counts that came through that never made it to my Spam folders, and were rejected and told "Please try again later" from the Spam checking.

How do I stop this flooding of emails coming into my chosen email?
I thought the email sent was supposed to be a total count after a certain amount of days, deleted from the spam folders?

Which line do I change for this to be correct?

You have any Knowledge on this one, ^Doom^?

Thanks

Ed

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-02-04 10:23

^DooM^ wrote:What version of hMail are you running it on? Are any errors reported in the error log or standard logs? you know the usual useful information!!
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

ed82264
New user
New user
Posts: 10
Joined: 2009-02-01 04:59
Location: Ma
Contact:

Re: My script to delete spam email after x days old, with report

Post by ed82264 » 2009-02-04 11:38

^DooM^ wrote:
^DooM^ wrote:What version of hMail are you running it on? Are any errors reported in the error log or standard logs? you know the usual useful information!!
I'm running 4.4.3-B285
The only thing not checked for logging is POP3, and I don't see any errors anywhere.

alainpp
New user
New user
Posts: 8
Joined: 2008-12-09 13:15

Re: My script to delete spam email after x days old, with report

Post by alainpp » 2009-03-02 16:24

Is there anyway to do the same thing with emails in the trash folder? or i just need to change the folder name in the script?

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-03-16 14:25

alainpp:
I believe all you'd have to do is put the correct folder name (IE: Trash/SPAM/Mail_I_Don't_Want) into the script and it should yank out the mail on that.

norx:
Here's the current Delete Spam script that I'm running. Feel free to take out the debug comments, I like a lot of status updates when I playing with new things and don't usually take them out. As noted, this is based on someone else's work, and I don't claim the credit.

Code: Select all

'DelSpamTrap.vbs - PMW - v1.0 - 07/21/2008
'03/05/2009 - added section to delete only up to the last DaysOfSpam days of email (including size/count in report)
'Based From
' DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam

AdminUser = "ADMINISTRATORUSER"	' Replace this with your administrator username
AdminPass = "PASSWORD"			' Replace this with your administrator password
FromEmail = "report@example.com"	' Replace this with the email address you want the report to come "from"
ReportToEmail = "user@example.com"	' Replace this with the email address you want the report to be sent to
SendSpamReport = False			' If true, each user gets an email listing all emails in their spam folder, if false, no email is sent
DaysOfSpam = 5

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim Message

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum

Dim OutputMsg

Dim NumMsgs
Dim AccountSize
Dim DeletedSize
Dim DeletedCnt

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

Set Domains = App.Domains
'Msgbox "DEBUG: Domains.Count = " & Domains.Count
For DomainNum = 1 To Domains.Count
	Set Domain = Domains.Item(DomainNum-1)
	'Msgbox "DEBUG: Domain.Active = " & Domain.Active
	If Domain.Active Then
		Set Accounts = Domain.Accounts
		'Msgbox "DEBUG: Accounts.Count = " & Accounts.Count
		For AccountNum = 1 To Accounts.Count
			Set Account = Accounts.Item(AccountNum-1)
			'Msgbox "DEBUG: Account.Address = " & Account.Address
			If UCase(Account.Address) = "SPAM-ACCOUNT@EXAMPLE.COM" Then
				'Msgbox "DEBUG: Account.Active = " & Account.Active
				If Account.Active = True Then
					Set IMAPFolders = Account.IMAPFolders
					'Msgbox "DEBUG: IMAPFolders.Count = " & IMAPFolders.Count
					For IMAPFolderNum = 1 To IMAPFolders.Count
						Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum-1)
						Set Messages = IMAPFolder.Messages
						NumMsgs = NumMsgs + Messages.Count
						'Msgbox "DEBUG: Messages.Count = " & Messages.Count
						If Messages.Count > 0 Then
							For MessageNum = Messages.Count To 1 Step -1
								'Msgbox "DEBUG: MessageNum = " & MessageNum
								Set Message = Messages.Item(MessageNum-1)
								'Msgbox "DEBUG: Message Set"
								AcountSize = AccountSize + Message.Size
								'Msgbox "DEBUG: Message Size Added"
								If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
									DeletedCnt = DeletedCnt + 1
									DeletedSize = DeletedSize + Message.Size
									Messages.DeleteByDBID Message.ID
									'Msgbox "DEBUG: Message Deleted"
								End If
							Next
						End If

					Next
					OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td><b>Total Size</b></td><td><b>Number Of<br/>Messages</b></td><td><b>Total Size<br/>Deleted</b></td><td><b>Number Of<br/>Deleted Messages</b></td></tr>" & vbCrLf
					OutputMsg = OutputMsg & "<tr><td>" & Domain.Name & "</td>"
					OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AcountSize, 0, True, False, True) & "K</td>"
					OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
					OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(DeletedSize, 0, True, False, True) & "K</td>"
					OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(DeletedCnt, 0, True, False, True) & "</td>" & vbCrLf
				Else
					'the account isn't active
					OutputMsg = "<font face=""Arial""><h1>The SPAM-ACCOUNT Acount for Domain: " & Domain.Name & " is not active."
				End If
			End If
		Next
	End If
Next

'Msgbox "DEBUG: Creating e-mail"
Set Message = CreateObject("hMailServer.Message")

Message.FromAddress = FromEmail
Message.From = "Spam Deletion Daemon"
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily Spam Account Deletion Report"
Message.HTMLBody = OutputMsg
Message.Save

'Msgbox "DEBUG: Sending e-mail"

Function BReplace(InputString)
	BReplace = Replace(Replace(InputString, "<", "<"), ">", ">")
End Function

alainpp
New user
New user
Posts: 8
Joined: 2008-12-09 13:15

Re: My script to delete spam email after x days old, with report

Post by alainpp » 2009-04-12 11:59

Thanks pmw,

i changed the folder name and it works.

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-24 10:01

hi guys,
Can you please help me with how to use this script if i need to delete emails from a particular account on a particular domain and not all domains. Will appreciate an early reply. thanks

Susheel

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-04-24 10:03

This post contains scripts for V4. If you are running V5 of hMail these may not work.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-24 10:10

Doom, am using hMail 4.

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-24 19:01

^DooM^: I'm using hMailServer 5 right now, I haven't noticed any problems, but I'll go through and check things again.

BTW: is there any specific changes that popped into your head where v4 and v5 differ?
also, how did you pick out that it's a v4 script? like I said, I found it and it worked on the hmailserver (v4) i had at the time.

susheel: I wrote a version to take out everything in one account (my spam catcher). Give me a bit to find it, and look through the version differences and i'll post it

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-04-24 19:22

I said they MAY not work.

How did i know it's for V4? It is in the V4 section of the forum. All pre V5 scripts were moved here to help prevent any incompatible scripts being used on V5. I only mentioned that this was a V4 script to save him some headache if it wasn't compatible. If you are using it on V5 you should post it in the V5 section in a new post.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-24 20:20

DooM: Yeah, noticed that a little later on. It's been a bad day anyway :)

susheel: this is how I would modify it to work how you were describing.
You have to tell it what domain you want, (if you only have one domain, you could probably yank all the domain stuff) and the account address you want to delete from.
This is only looking for a single specified folder.

I have NOT tested this and WON'T promise it works, but at least it should get you on the way
Other standard disclaimers like it's not my original code, etc.

Code: Select all

'Single Selected Domain Delete Spam v1.0
'PMW
'4/24/09
'based on DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam

AdminUser = "ADMINUSER"						'enter hMailServer administrator login name
AdminPass = "ADMINPASSWORD"					'enter hMailServer administrator password
FromEmail = "mailer-daemon@EXAMPLE.COM"			'enter the address the email is "sent" from
ReportToEmail = "ADMIN@YOUR-DOMAIN.COM"			'enter the address to send the report
DaysOfSpam = 7								'how many days old spam is allowed to live	(default = 7)

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim Message

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum

Dim AccountMsg
Dim DomainMsg
Dim ReportMsg

Dim AccountSpamCnt
Dim AccountSpamSz
Dim AccountDeletedCnt
Dim AccountDeletedSz
Dim DomainAccounts
Dim DomainSpamCnt
Dim DomainSpamSz
Dim DomainDeletedCnt
Dim DomainDeletedSz
Dim ReportDomains
Dim ReportSpamCnt
Dim ReportSpamSz
Dim ReportDeletedCnt
Dim ReportDeletedSz

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

ReportSpamCnt = 0
ReportSpamSz = 0
ReportDeletedCnt = 0
ReportDeletedSz = 0
ReportSpamCnt = 0

Set Domains = App.Domains
'Msgbox "DEBUG: Domains.Count = " & Domains.Count
ReportDomains = Domains.Count
For DomainNum = 1 To Domains.Count
	DomainSpamCnt = 0
	DomainSpamSz = 0
	DomainDeletedCnt = 0
	DomainDeletedSz = 0
	Set Domain = Domains.Item(DomainNum - 1)

        If UCase(Domain.Name) = "EXAMPLE.COM" then

'	        Msgbox "DEBUG: Domain.Active = " & Domain.Active
		If Domain.Active Then
			Set Accounts = Domain.Accounts

			AccountMsg = "<table border=""1"" style=""margin-left: auto; margin-right: auto;"">" & vbCrLf
			AccountMsg = AccountMsg & "<tr><th>Account</th><th>Spam Count</th><th>Spam Size</th><th>Spam Deleted</th><th>Size Deleted</th></tr>" & vbCrLf

'			Msgbox "DEBUG: Accounts.Count = " & Accounts.Count
			DomainAccounts = Accounts.Count
			For AccountNum = 1 To Accounts.Count
				AccountSpamCnt = 0
				AccountSpamSz = 0
				AccountDeletedCnt = 0
				AccountDeletedSz = 0
			
				Set Account = Accounts.Item(AccountNum - 1)

				If UCase(Account.Address) = "ADDRESS_TO_DELETE_FROM@EXAMPLE.COM" Then

'					Msgbox "DEBUG: Account.Active = " & Account.Active				
					If Account.Active Then				
						Set IMAPFolders = Account.IMAPFolders
						'find spam, delete it, make the report
'						Msgbox "DEBUG: IMAPFolders.Count = " & IMAPFolders.Count
						For IMAPFolderNum = 1 To IMAPFolders.Count
							Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
'							Msgbox "DEBUG: IMAPFolder.Name = " & IMAPFolder.Name
							If UCase(IMAPFolder.Name) = "FOLDER_TO_DELETE_FROM" Then 
								Set Messages = IMAPFolder.Messages
'								Msgbox "DEBUG: Messages.Count = " & Messages.Count
								AccountSpamCnt = AccountSpamCnt + Messages.Count
'								Msgbox "DEBUG: AccountSpamCnt = " & AccountSpamCnt
								For MessageNum = Messages.Count To 1 Step -1
'									Msgbox "DEBUG: MessageNum = " & MessageNum & " of " & Messages.Count
									Set Message = Messages.Item(MessageNum-1)
									AccountSpamSz = AccountSpamSz + Message.Size
'									Msgbox "DEBUG: AccountSpamSz = " & AccountSpamSz
									If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
										AccountDeletedCnt = AccountDeletedCnt + 1
'										Msgbox "DEBUG: AccountDeletedCnt = " & AccountDeletedCnt
										AccountDeletedSz = AccountDeletedSz + Message.Size
'										Msgbox "DEBUG: AccountDeletedSz = " & AccountDeletedSz
										Messages.DeleteByDBID Message.ID
									End If
								Next
							End If
						Next
						AccountMsg = AccountMsg &"<tr style=""text-align: center;""><td>" & Account.Address & "</td><td>" & FormatNumber(AccountSpamCnt,0,True,False,True) & "</td><td>" & FormatNumber(AccountSpamSz,0,True,False,True) & "Kb</td><td>" & FormatNumber(AccountDeletedCnt,0,True,False,True) & "</td><td>" & FormatNumber(AccountDeletedSz,0,True,False,True) & "Kb</td></tr>" & vbCrLf
					Else
						'report disabled account
'						Msgbox "DEBUG: Account.Address = " & Account.Address & " Disabled"
						AccountMsg = AccountMsg & "<tr style=""text-align: center;""><td>" & Account.Address & "</td><td colspan=""4"">Account Not Active</td></tr>" & vbCrLf
					End If
					DomainSpamCnt = DomainSpamCnt + AccountSpamCnt
					DomainSpamSz = DomainSpamSz + AccountSpamSz
					DomainDeletedCnt = DomainDeletedCnt + AccountDeletedCnt
					DomainDeletedSz = DomainDeletedSz + AccountDeletedSz
					
				End If
				
			Next
			AccountMsg = AccountMsg & "</table>" & vbCrLf
			
		End If
		
	End If
	ReportSpamCnt = ReportSpamCnt + DomainSpamCnt
	ReportSpamSz = ReportSpamSz + DomainSpamSz
	ReportDeletedCnt = ReportDeletedCnt + DomainDeletedCnt
	ReportDeletedSz = ReportDeletedSz + DomainDeletedSz

	DomainMsg = "<table style=""margin-left: auto; margin-right: auto;"">" & vbCrLf
	DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Domain: </th><td>" & Domain.Name & "</td></tr>" & vbCrLf
	If Domain.Active Then
		DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Total Accounts: </th><td>" & FormatNumber(DomainAccounts,0,True,False,True) & "</td></tr>" & vbCrLf
		DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Domain Spam Count: </th><td>" & FormatNumber(DomainSpamCnt,0,True,False,True) & "</td></tr>" & vbCrLf
		DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Domain Spam Size: </th><td>" & FormatNumber(DomainSpamSz,0,True,False,True) & "Kb</td></tr>" & vbCrLf
		DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Domain Spam Deleted: </th><td>" & FormatNumber(DomainDeletedCnt,0,True,False,True) & "</td></tr>" & vbCrLf
		DomainMsg = DomainMsg & "<tr><th style=""text-align: right;"">Domain Deleted Size: </th><td>" & FormatNumber(DomainDeletedSz,0,True,False,True) & "Kb</td></tr>" & vbCrLf
	Else
		DomainMsg = DomainMsg & "<tr><th colspan=""2"">Domain is not Active</td></tr>" & vbCrLf
	End If
	DomainMsg = DomainMsg & "</table>" & vbCrLf
	
	DomainMsg = DomainMsg & AccountMsg
Next

ReportMsg = "<h1 style=""text-align: center; font-weight: bold"">Spam Report For " & FormatDateTime(Date(),vbLongDate) & "</h1>" & vbCrLf

ReportMsg = ReportMsg & DomainMsg

ReportMsg = ReportMsg & "<table style=""margin-left: auto; margin-right: auto;"">" & vbCrLf
ReportMsg = ReportMsg & "<tr><th style=""text-align: right;"">Total Domains: </td><td>" & FormatNumber(ReportDomains,0,True,False,True) & "</td></tr>" & vbCrLf
ReportMsg = ReportMsg & "<tr><th style=""text-align: right;"">Total Spam Count: </td><td>" & FormatNumber(ReportSpamCnt,0,True,False,True) & "</td></tr>" & vbCrLf
ReportMsg = ReportMsg & "<tr><th style=""text-align: right;"">Total Spam Size: </td><td>" & FormatNumber(ReportSpamSz,0,True,False,True) & "Kb</td></tr>" & vbCrLf
ReportMsg = ReportMsg & "<tr><th style=""text-align: right;"">Total Spam Deleted: </td><td>" & FormatNumber(ReportDeletedCnt,0,True,False,True) & "</td></tr>" & vbCrLf
ReportMsg = ReportMsg & "<tr><th style=""text-align: right;"">Total Deleted Size: </td><td>" & FormatNumber(ReportDeletedSz,0,True,False,True) & "Kb</td></tr>" & vbCrLf
ReportMsg = ReportMsg & "</table>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.FromAddress = FromEmail
Message.From = "Spam Deletion Daemon"
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily Spam Deletion Report"
Message.HTMLBody = ReportMsg
Message.Save


susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-26 22:13

I pmw. Thanks for your time and the script but unfortunately it isn't working :( Also fyi, i need to delete emails from INBOX every couple of days.. Will you be kind enough to help me out? Thanks - Susheel

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-27 01:29

susheel: how is it not working?
like I noted, I haven't tested it, but it should be pretty much the same script posted earlier with a test for the account address
just to clear out the obvious (keep being told I assume too much)
did you change the account address and folder name?
Also, if this script works for one folder, you can set it for another (have 2 copies, one for spam that runs every day and one for inbox that runs every couple of days)

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-27 09:03

Yes pmw, i changed the folder named as "INBOX" and the email address as well. I saved the file as a vbs script and ran it in command prompt. A screen came up and just vanished. Am i doing something wrong here?

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-27 12:12

try this:
cscript "full_path_to_scriptfile.vbs"

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-27 15:13

this is what i get
C:\Program Files\Parallels\Plesk\Mail Servers\hMailServer\Events>cscript ftbomba
y.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.


C:\Program Files\Parallels\Plesk\Mail Servers\hMailServer\Events>

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-27 15:40

well, assuming the script is called ftbombay.vbs and is in the C:\Program Files\Parallels\Plesk\Mail Servers\hMailServer\Events folder, then I would check your e-mail. This script doesn't produce anything for output but an e-mail to the specified e-mail address(es)

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-29 19:55

its simply not working. I have given up!! The old script i.e. 'DelOldSpam.vbs" is working though. It created spam folders in all domains and email ids and i also did get a email with a report.

Can we just edit the script for one domain to delete emails from INBOX after particular time/date? Will really appreciate a help!!!

DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008
' Some Edits made by Chris Dixon - 09/08/2008 using some scripting found in forum

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-29 22:36

Yeah, I'll see if I can get something going. Is it the same script from the top of this topic (gmaclean's script)? If not, then I'll need the current copy your using at the second.

I'm a little confused about why you'd want to indiscriminately wipe the inbox based on time, but that's your deal.

Also, as a last ditch, here's the last version I worked on. I have tested this one and it was getting the messages from one top-level folder in my account on my domain. I believe that was the requirement.

Code: Select all

'Selected Domain, Specific User, Specifc Folder Email Deletion v1.1
'PMW
'4/29/09
'based on DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008

AdminUser = "ADMIN_USER"						'enter hMailServer administrator login name
AdminPass = "ADMIN_PASSWORD"					'enter hMailServer administrator password
FromEmail = "SOMETHING@YOUR_DOMAIN.com"		'enter the address the email is "sent" from
ReportToEmail = "ADMIN_EMAIL"					'enter the address to send the report
DaysOfSpam = 7								'how many days old spam is allowed to live	(default = 7)
SelectedDomain = "YOUR_DOMAIN.com"				'replace this with the domain name to delete content from
SelectedFolder = "YOUR FOLDER"					'replace this with the account folder to delete content from
SelectedAccount = "YOUR_ACCOUNT@YOUR_DOMAIN.com"	'replace this with the account address to delete content from

TestingScript = TRUE		'use this to test the script, will e-mail with a list of the subject lines to be deleted and sizes
ListMessages = TRUE		'use this to keep the list of e-mail subject lines and sizes when not testing the script (will delete the messaes if TestingScript is not TRUE)

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

ReportMsgCnt = 0
ReportMsgSz = 0
ReportDeletedCnt = 0
ReportDeletedSz = 0

ReportMsg = ""

Set Domains = App.Domains
'Msgbox "DEBUG: Domains.Count = " & Domains.Count
For DomainNum = 1 To Domains.Count
'Msgbox "DEBUG: DomainNum = " & DomainNum
	Set Domain = Domains.Item(DomainNum-1)
	DomainMsg = ""
	DomainMsgCnt = 0
	DomainMsgSz = 0
	DomainDeletedCnt = 0
	DomainDeletedSz = 0
'	Msgbox "DEBUG: Domain.Name = " & Domain.Name
	If UCase(Domain.Name) = UCase(SelectedDomain) Then
'		Msgbox "DEBUG: Domain.Active = " & Domain.Active
		If Domain.Active Then
			Set Accounts = Domain.Accounts
'			Msgbox "DEBUG: Accounts.Count = " & Accounts.Count
			For AccountNum = 1 To Accounts.Count
'				Msgbox "DEBUG: AccountNum = " & AccountNum
				Set Account = Accounts.Item(AccountNum-1)
				AccountMsg = ""
				AccountMsgCnt = 0
				AccountMsgSz = 0
				AccountDeletedCnt = 0
				AccountDeletedSz = 0
'				Msgbox "DEBUG: Account.Address = " & Account.Address
				If UCase(Account.Address) = UCase(SelectedAccount) Then
'					Msgbox "DEBUG: Account.Active = " & Account.Active
					If Account.Active Then
						Set IMAPFolders = Account.IMAPFolders
						For FolderNum = 1 To IMAPFolders.Count
'							Msgbox "DEBUG: FolderNum = " & FolderNum
							Set Folder = IMAPFolders(FolderNum-1)
'							Msgbox "DEBUG: Folder.Name = " & Folder.Name
							If UCase(Folder.Name) = UCase(SelectedFolder) Then
								Set Messages = Folder.Messages
								TestingMsg = "<tr><td colspan=""5"" style=""text-align: left;"">"
								For MessageNum = Messages.Count To 1 Step -1
'									Msgbox "DEBUG: MessageNum = " & MessageNum
									Set Message = Messages.Item(MessageNum-1)
'									Msgbox "DEBUG: Message.Subject = " & Message.Subject
									AccountMsgCnt = AccountMsgCnt + 1
									AccountMsgSz = AccountMsgSz + Message.Size
									If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
										AccountDeletedCnt = AccountDeletedCnt + 1
										AccountDeletedSz = AccountDeletedSz + Message.Size
										If TestingScript = True Then
											TestingMsg = TestingMsg & Message.Subject & " would be deleted from " & Folder.Name & " on " & Account.Address & " (" & Message.Size & " kB)<br>"
										Else
											If ListMessages = True Then
												TestingMsg = TestingMsg & Message.Subject & " deleted from " & Folder.Name & " on " & Account.Address & " (" & Message.Size & " kB)<br>"
											End If
											Messages.DeleteByDBID Message.ID
										End If
'										Msgbox "DEBUG: " & Message.Subject & " Deleted"
									End If
								Next
								TestingMsg = TestingMsg & "</td></tr>" & vbCrLf
							End If
						Next
						If ((TestingScript = True) Or (TestingScript = False and ListMessages = True)) Then
							AccountMsg = "<tr><td style=""text-align: right;"">" & Account.Address & "</td><td style=""text-align: center;"">" & AccountMsgCnt & "</td><td style=""text-align: center;"">" & AccountMsgSz & " kB</td><td style=""text-align: center;"">" & AccountDeletedCnt & "</td><td style=""text-align: center;"">" & AccountDeletedSz & " kB</td></tr>" & vbCrLF & TestingMsg
						Else
							AccountMsg = "<tr><td style=""text-align: right;"">" & Account.Address & "</td><td style=""text-align: center;"">" & AccountMsgCnt & "</td><td style=""text-align: center;"">" & AccountMsgSz & " kB</td><td style=""text-align: center;"">" & AccountDeletedCnt & "</td><td style=""text-align: center;"">" & AccountDeletedSz & " kB</td></tr>" & vbCrLF
						End If
					Else
						AccountMsg = "<tr><td colspan=""5"" style=""text-align: center;"">Account " & Account.Address & " not active</td></tr>" & vbCrLf
					End If
				End If
				DomainMsgCnt = DomainMsgCnt + AccountMsgCnt
				DomainMsgSz = DomainMsgSz + AccountMsgSz
				DomainDeletedCnt = DomainDeletedCnt + AccountDeletedCnt
				DomainDeletedSz = DomainDeletedSz + AccountDeletedSz
				DomainMsg = DomainMsg & AccountMsg
			Next
			DomainMsg = "<tr><td style=""text-align: left;"">Report for " & Domain.Name & "</td><td style=""text-align: center;"">" & DomainMsgCnt & "</td><td style=""text-align: center;"">" & DomainMsgSz & " kB</td><td style=""text-align: center;"">" & DomainDeletedCnt & "</td><td style=""text-align: center;"">" & DomainDeletedSz & " kB</td></tr>" & vbCrLf & DomainMsg
		Else
			DomainMsg = "<tr><td colspan=""5"" style=""text-align: center;"">Domain " & Domain.Name & " not active</td></tr>" & vbCrLf
		End If
	End If
	ReportMsgCnt = ReportMsgCnt + DomainMsgCnt
	ReportMsgSz = ReportMsgSz + DomainMsgSz
	ReportDeletedCnt = ReportDeletedCnt + DomainDeletedCnt
	ReportDeletedSz = ReportDeletedSz + DomainDeletedSz
	ReportMsg = ReportMsg & DomainMsg
Next
ReportMsg = "<table cols=""5"" border=""1"" align=""center""><tr><td colspan=""5"" style=""text-align: center;"">Deletion Report: " & SelectedFolder & " on " & SelectedAccount & " in " & SelectedDomain & "</td></tr>" & vbCrLf & "<tr><th>&nbsp;</th><th>Messages<br>Counted</th><th>Size (kB)</th><th>Messages<br>Deleted</th><th>Size (kB)</th></tr>" & vbCrLf & ReportMsg & "</table>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.FromAddress = FromEmail
Message.From = "Spam Deletion Daemon"
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily Spam Deletion Report: " & SelectedFolder & " of " & SelectedAccount & " on " & SelectedDomain
Message.HTMLBody = ReportMsg
Message.Save
Send me the code you're working with and I'll poke at it probably this weekend.

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-30 08:40

Code: Select all

' DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008
' Some Edits made by Chris Dixon - 09/08/2008 using some scripting found in forum

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam
Dim DaysAfterSpam ' Added by Chris Dixon

AdminUser = "Administrator" ' Replace this with your administrator username
AdminPass = "xxxxxxx"  ' Replace this with your administrator password
FromEmail = "admin@xxxx.com" ' Replace this with the email address you want the report to come "from"
ReportToEmail = "user@xxxxxx.com" ' Replace this with the email address you want the report to be sent to
DaysOfSpam = 7 ' Replace this with the number of days spam is kept before being deleted
DaysAfterSpam = 2 ' Replace this with the number of days future date spam is kept before deleted - Added by Chris Dixon
SendSpamReport = False ' If true, each user gets an email listing all emails in their spam folder, if false, no email is sent

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim oMessages ' Added by Chris Dixon
Dim Message
Dim oMessage ' Added by Chris Dixon
Dim vbDate ' Added by Chris Dixon

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum
Dim oMessageNum ' Added by Chris Dixon

Dim FoundSpamFolder

Dim OutputMsg

Dim NumMsgs
Dim AccountSize
Dim NumSpams
Dim NumDeleted
Dim TotalAccounts
Dim TotalNumMsgs
Dim TotalAccountSize
Dim TotalNumSpams
Dim TotalNumDeleted

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

TotalNumMsgs = 0
TotalAccountSize = 0
TotalNumSpams = 0
TotalNumDeleted = 0

Set Domains = App.Domains

For DomainNum = 1 To Domains.Count
  Set Domain = Domains.Item(DomainNum - 1)
  
  If Domain.Active Then
  
    Set Accounts = Domain.Accounts
    
    NumMsgs = 0
    AccountSize = 0
    NumSpams = 0
    NumDeleted = 0
    
    For AccountNum = 1 To Accounts.Count
      Set Account = Accounts.Item(AccountNum - 1)
  
      If Account.Active Then
        
        Set IMAPFolders = Account.IMAPFolders
        
        FoundSpamFolder = False
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
          If UCase(IMAPFolder.Name) = "SPAM" Then 
            FoundSpamFolder = True
            Exit For
          End If
        Next
        
        If Not FoundSpamFolder Then Set IMAPFolder = IMAPFolders.Add("spam")
        
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)

          Set Messages = IMAPFolder.Messages

          If UCase(IMAPFolder.Name) = "SPAM" Then 
            NumSpams = NumSpams + Messages.Count
            TotalNumSpams = TotalNumSpams + Messages.Count
          End If

          NumMsgs = NumMsgs + Messages.Count
          TotalNumMsgs = TotalNumMsgs + Messages.Count
          
          For MessageNum = Messages.Count To 1 Step -1
            Set Message = Messages.Item(MessageNum - 1)

            AccountSize = AccountSize + Message.Size
            TotalAccountSize = TotalAccountSize + Message.Size
            
            If UCase(IMAPFolder.Name) = "SPAM" Then
              If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
                NumDeleted = NumDeleted + 1
                TotalNumDeleted = TotalNumDeleted + 1
                
                Messages.DeleteByDBID Message.ID
              End If
            End If
          Next

          ' Start of Added Code by Chris Dixon

          Set oMessages  = Account.IMAPFolders.ItemByName("spam").Messages

          For oMessageNum = oMessages.Count To 1 Step -1
            Set oMessage = oMessages.Item(oMessageNum - 1)
            
            If (vbDate > CDate(Now + DaysAfterSpam)) Then
              NumDeleted = NumDeleted + 1
              TotalNumDeleted = TotalNumDeleted + 1
              
              oMessages.DeleteByDBID(oMessage.ID)
            End If
          Next
          
          ' End of Added Code by Chris Dixon
          
          If UCase(IMAPFolder.Name) = "SPAM" And SendSpamReport Then
            Set Messages = IMAPFolder.Messages
            If Messages.Count > 0 Then
              SpamMsg = "<font face=""Arial""><p>The following " & Messages.Count & " messages are currently being held in your Spam mail folder. "
              SpamMsg = SpamMsg & "Any messages older than " & DaysOfSpam & " days will be deleted.</p>" & vbCrLf
  
              SpamMsg = SpamMsg & "<table border=""1""><tr><td><b>From</b></td><td><b>Date</b></td><td><b>Subject</b></td></tr>" & vbCrLf
  
              For MessageNum = Messages.Count To 1 Step -1
                Set Message = Messages.Item(MessageNum - 1)
                SpamMsg = SpamMsg & "<tr><td>" & BReplace(Message.From) & "</td><td>"
                SpamMsg = SpamMsg & FormatDateTime(Message.InternalDate,vbShortDate) & " " & FormatDateTime(Message.InternalDate,vbLongTime) & "</td>"
                SpamMsg = SpamMsg & "<td>" & BReplace(Message.Subject) & "</td></tr>" & vbCrLf
              Next
              
              SpamMsg = SpamMsg & "</table></font>"

              Set Message = CreateObject("hMailServer.Message")
              
              Message.From = "Spam Deletion Daemon <" & FromEmail & ">"
              Message.FromAddress = FromEmail
              Message.AddRecipient "DixonVision Network User", Account.Address
              Message.Subject = "Daily Spam Folder Report"
              Message.HTMLBody = SpamMsg
              Message.Save
            
            End If
          End If
          
        Next          
      End If
    Next

    
    TotalAccounts = TotalAccounts + Accounts.Count
    
    OutputMsg = OutputMsg & "<tr><td>" & Domain.Name & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(Accounts.Count, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AccountSize, 0, True, False, True) & "K</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumSpams, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf
    
  End If
Next


OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td align=""right""><b>Users</b></td><td align=""right""><b>Size</b></td><td align=""right""><b>Messages</b></td><td align=""right""><b>Spam</b></td><td align=""right""><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg

OutputMsg = OutputMsg & "<tr><td><b>Total</b></td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccounts, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccountSize, 0, True, False, True) & "K</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumMsgs, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumSpams, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf

OutputMsg = OutputMsg & "</table></font>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.From = "Spam Deletion Daemon <" & FromEmail & ">"
Message.FromAddress = FromEmail
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily spam deletion report"
Message.HTMLBody = OutputMsg
Message.Save

Function BReplace(InputString)

  BReplace = Replace(Replace(InputString, "<", "<"), ">", ">")

End Function


susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-04-30 08:42

hi Pmw, I have pasted the code. I also used your code but nothing is working unfortunately :(

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-04-30 11:04

What errors are you getting? Could be that you copied and pasted the word wrapped lines and they ended up on new lines. Perhaps PMW could attach a txt file instead.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-30 14:08

DooM: Cool, but I'm getting "The extention is not allowed" when I try to attach txt or vbs files. Even took the extention off and it got mad.
What's the magic extension?

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-04-30 18:13

zip it up :)
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-04-30 19:16

DooM: Awesome. Can't tell I play on forums all day huh?
:)

susheel: I have copied the code to my machine so I can look at it. When I get something, I'll post it to you.

Really want to know why that script isn't running....
Attachments
DelSelDomUsrFldr.zip
hMailServer 4/5 script to delete old mail in a specific folder of a specific user on a specific domain (zipfile)
(1.76 KiB) Downloaded 551 times

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-05-01 00:45

susheel: Ok, this is the code I worked up.
Sorry to sound patronizing, but I just want to be sure. You do know that this code isn't picking out spam and not spam, it's deleting e-mail that's older than a number of days, regardless. I'm mentioning this because you said you wanted to run this on your inbox, and I just want to be sure you know what is going on.

the code below is posted as a zip file on this message.
usual disclaimers, not mine, blah blah
worked on my system, but I didn't have any future spam to try it with

original code was commented out when i changed it, my code was just stuck in

Code: Select all

' DelOldSpam.vbs - Scott MacLean - v1.1 - 07/11/2008
' Some Edits made by Chris Dixon - 09/08/2008 using some scripting found in forum
' More edits made by PMW - 04/30/2009 at the request of susheel

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam
Dim DaysAfterSpam ' Added by Chris Dixon

AdminUser = "Administrator" ' Replace this with your administrator username
AdminPass = "xxxxxxxx"  ' Replace this with your administrator password
FromEmail = "admin@xxxx.com" ' Replace this with the email address you want the report to come "from"
ReportToEmail = "admin@xxxx.com" ' Replace this with the email address you want the report to be sent to
DaysOfSpam = 0 ' Replace this with the number of days spam is kept before being deleted
DaysAfterSpam = 2 ' Replace this with the number of days future date spam is kept before deleted - Added by Chris Dixon
SendSpamReport = False ' If true, each user gets an email listing all emails in their spam folder, if false, no email is sent

Dim YourDomain
Dim YourAccount
Dim YourFolder

YourDomain = "xxxx.com" 'replace with your domain (should be same as above)
YourAccount = "user@xxxx.com" 'for most users, this should be the same as ReportToEmail
YourFolder = "Spam" 'this should be changed to the folder to delete e-mails from

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim oMessages ' Added by Chris Dixon
Dim Message
Dim oMessage ' Added by Chris Dixon
Dim vbDate ' Added by Chris Dixon

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum
Dim oMessageNum ' Added by Chris Dixon

Dim FoundSpamFolder

Dim OutputMsg

Dim NumMsgs
Dim AccountSize
'Dim NumSpams	'useless since we're not deleting spam
Dim NumDeleted
'Dim TotalAccounts	'useless since we're only deleting one account
'Dim TotalNumMsgs	'useless since we're only deleting one account
'Dim TotalAccountSize	'useless since we're only deleting one account
'Dim TotalNumSpams	'useless since we're not deleting spam
'Dim TotalNumDeleted	'useless since we're only deleting one account

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

'TotalNumMsgs = 0	'useless since we're only deleting one account
'TotalAccountSize = 0	'useless since we're only deleting one account
'TotalNumSpams = 0	'useless since we're not deleting spam
'TotalNumDeleted = 0	'useless since we're only deleting one account

Set Domains = App.Domains

For DomainNum = 1 To Domains.Count
  Set Domain = Domains.Item(DomainNum - 1)
  
  If UCase(Domain.Name) = UCase(YourDomain) Then
 
    If Domain.Active Then
 
      Set Accounts = Domain.Accounts

      NumMsgs = 0
      AccountSize = 0
      'NumSpams = 0  'useless since we're not deleting spam
      NumDeleted = 0
   
      For AccountNum = 1 To Accounts.Count
        Set Account = Accounts.Item(AccountNum - 1)
        
        If UCase(Account.Address) = UCase(YourAccount) Then
        
          If Account.Active Then
       
            Set IMAPFolders = Account.IMAPFolders
       
            'FoundSpamFolder = False
            'For IMAPFolderNum = 1 To IMAPFolders.Count
              'Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
              'If UCase(IMAPFolder.Name) = "SPAM" Then
                'FoundSpamFolder = True
                'Exit For
              'End If
            'Next
       
            'If Not FoundSpamFolder Then Set IMAPFolder = IMAPFolders.Add("spam")
       
            For IMAPFolderNum = 1 To IMAPFolders.Count
              Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
              
              If UCase(IMAPFolder.Name) = UCase(YourFolder) Then

                Set Messages = IMAPFolder.Messages

                'If UCase(IMAPFolder.Name) = "SPAM" Then
                  'NumSpams = NumSpams + Messages.Count
                  'TotalNumSpams = TotalNumSpams + Messages.Count
                'End If

                NumMsgs = NumMsgs + Messages.Count
                'TotalNumMsgs = TotalNumMsgs + Messages.Count
         
                For MessageNum = Messages.Count To 1 Step -1
                  Set Message = Messages.Item(MessageNum - 1)

                  AccountSize = AccountSize + Message.Size
                  'TotalAccountSize = TotalAccountSize + Message.Size
           
                  'If UCase(IMAPFolder.Name) = "SPAM" Then
                    If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
                      NumDeleted = NumDeleted + 1
                      'TotalNumDeleted = TotalNumDeleted + 1
               
                      Messages.DeleteByDBID Message.ID
                    ElseIf (vbDate > CDate(Now + DaysAfterSpam)) Then
                      NumDeleted = NumDeleted + 1
                      'TotalNumDeleted = TotalNumDeleted + 1
               
                      Messages.DeleteByDBID Message.ID
                    End If
                  'End If
                Next

                ' Start of Added Code by Chris Dixon

                'Set oMessages  = Account.IMAPFolders.ItemByName("spam").Messages
                                
                'For oMessageNum = oMessages.Count To 1 Step -1
                  'Set oMessage = oMessages.Item(oMessageNum - 1)
           
                  'If (vbDate > CDate(Now + DaysAfterSpam)) Then
                    'NumDeleted = NumDeleted + 1
                    'TotalNumDeleted = TotalNumDeleted + 1
             
                    'oMessages.DeleteByDBID(oMessage.ID)
                  'End If
                'Next
         
                ' End of Added Code by Chris Dixon
         
                'If UCase(IMAPFolder.Name) = "SPAM" And SendSpamReport Then
                If SendSpamReport Then
                  Set Messages = IMAPFolder.Messages
                  If Messages.Count > 0 Then
                    'SpamMsg = "<font face=""Arial""><p>The following " & Messages.Count & " messages are currently being held in your Spam mail folder. "
                    SpamMsg = "<font face=""Arial""><p>The following " & Messages.Count & " messages are currently being held in your " & YourFolder & " mail folder. "
                    SpamMsg = SpamMsg & "Any messages older than " & DaysOfSpam & " days will be deleted.</p>" & vbCrLf
 
                    SpamMsg = SpamMsg & "<table border=""1""><tr><td><b>From</b></td><td><b>Date</b></td><td><b>Subject</b></td></tr>" & vbCrLf
 
                    For MessageNum = Messages.Count To 1 Step -1
                      Set Message = Messages.Item(MessageNum - 1)
                      SpamMsg = SpamMsg & "<tr><td>" & BReplace(Message.From) & "</td><td>"
                      SpamMsg = SpamMsg & FormatDateTime(Message.InternalDate,vbShortDate) & " " & FormatDateTime(Message.InternalDate,vbLongTime) & "</td>"
                      SpamMsg = SpamMsg & "<td>" & BReplace(Message.Subject) & "</td></tr>" & vbCrLf
                    Next
             
                    SpamMsg = SpamMsg & "</table></font>"

                    Set Message = CreateObject("hMailServer.Message")
             
                    Message.From = "Spam Deletion Daemon <" & FromEmail & ">"
                    Message.FromAddress = FromEmail
                    Message.AddRecipient "DixonVision Network User", Account.Address
                    Message.Subject = "Daily Spam Folder Report"
                    Message.HTMLBody = SpamMsg
                    Message.Save
           
                  End If
                End If
              End If
            Next
          End If
        End If
      Next

      'TotalAccounts = TotalAccounts + Accounts.Count
   
      OutputMsg = OutputMsg & "<tr><td>" & Domain.Name & "</td>"
      'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(Accounts.Count, 0, True, False, True) & "</td>"
      OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AccountSize, 0, True, False, True) & "K</td>"
      OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
      'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumSpams, 0, True, False, True) & "</td>"
      OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf
   
    End If
  End If
Next


'OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td align=""right""><b>Users</b></td><td align=""right""><b>Size</b></td><td align=""right""><b>Messages</b></td><td align=""right""><b>Spam</b></td><td align=""right""><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg
OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td align=""right""><b>Size</b></td><td align=""right""><b>Messages</b></td><td align=""right""><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg

'OutputMsg = OutputMsg & "<tr><td><b>Total</b></td>"
'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccounts, 0, True, False, True) & "</td>"
'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccountSize, 0, True, False, True) & "K</td>"
'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumMsgs, 0, True, False, True) & "</td>"
'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumSpams, 0, True, False, True) & "</td>"
'OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf

OutputMsg = OutputMsg & "</table></font>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.From = "Spam Deletion Daemon <" & FromEmail & ">"
Message.FromAddress = FromEmail
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily " & YourFolder & " deletion report"
Message.HTMLBody = OutputMsg
Message.Save

Function BReplace(InputString)

  BReplace = Replace(Replace(InputString, "<", "<"), ">", ">")

End Function
Attachments
susheelcode.zip
zip file of above code
(2.33 KiB) Downloaded 563 times

susheel
Normal user
Normal user
Posts: 51
Joined: 2008-07-04 21:09

Re: My script to delete spam email after x days old, with report

Post by susheel » 2009-05-01 08:17

Whoa!!! Yes it worked.. Thank you soooooooo much pmw!! Thank you for your time an efforts.. really appreciate it :)
Just incase i need to run this for various other domains, do i need to create another script or add a list of domains out there in the script? If yes, please let me know where do i add them. Thanks again

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-05-01 12:40

it runs on one domain
it's not designed to selectively run on different domains
so you'd have to make copies of the script and put the correct domain in the field at the top

spudsrus
New user
New user
Posts: 7
Joined: 2009-02-10 09:56

Re: My script to delete spam email after x days old, with report

Post by spudsrus » 2009-05-29 06:24

hey guys, I'm here to bug you all for help again!

i need to do exactly what susheel needed for every user in a domain.
just the one domain and preferably filtering out a few accounts so they aren't affected.
i had a tinker with the code but it's scarily complex!!
at the moment I'm just getting blank emails with the table listing 0 messages and 0 deleted.

any ideas ? i can post my code if needed but honestly it's so terrible that i imagine it would be quicker starting from where you smart people left off.

my thought was just to try and apply the bottom half of pmws code for susheel with the top half of an earlier one so it applies to every user in the domain, then add in a case var where it checks the accounts so the accounts you don't want purged of old emails are excluded.

thanks in advance

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-05-29 13:47

Modifing this code to handle all accounts on a domain isn't a problem. Take out one of the if checks and the associated loop
Excepting accounts is not so easy.
Just off the top of my head, the easiest way to do it would be to change the account if to a IF NOT and compare

ie:

Code: Select all

IF account <> "account1@example.com" AND account <> "account2@example.com" etc then
just going from the top of my head.

spudsrus
New user
New user
Posts: 7
Joined: 2009-02-10 09:56

Re: My script to delete spam email after x days old, with report

Post by spudsrus » 2009-05-30 07:46

Code: Select all

        If UCase(Account.Address) = UCase(YourAccount) Then
that would be the right if statement to take out, yes ? and then i'll add that one of yours and leave the then blank and have the rest of the code following the else i suppose...

does that sound about right ?

i'll try it over the weekend though i won't have proper access to my server until tuesday

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-05-31 15:27

The quick and dirty way would be to take that line and change it so that it reads

Code: Select all

If UCase(Account.Address) <> UCase("ExceptedAccount1@exampledomain.com") AND UCase(Account.Address) <> UCase("ExceptedAccount2@exampledomain.com") AND etc... Then
The reason I'm saying it'd be dirty is because you'd have to have a AND <> for each account you want to exclude from processing. If you have to go across multiple domains you'd have to list all the accounts for all the domains.

Also, this script would wipe out all the mail (over X days old) in the accounts that are not excepted

spudsrus
New user
New user
Posts: 7
Joined: 2009-02-10 09:56

Re: My script to delete spam email after x days old, with report

Post by spudsrus » 2009-06-02 09:48

edited the script a little, it all seems to be working with totals and such, thanks heaps for the help... I'll let you know if i run into any serious problems...!

=]

JoNtheOueB
New user
New user
Posts: 29
Joined: 2006-05-11 20:07
Location: FRANCE (Marne La Vallée - 77)
Contact:

Re: My script to delete spam email after x days old, with report

Post by JoNtheOueB » 2009-07-09 10:42

I don't understand, because I wanted to use this script with my 4.4.1 B273 hMailServer, and I have a problem.

I just have replaced the folder name in the script, because min e is called "Junk" instead of "Spam". And I have an error message :
Line : 82
Caract. : 37
Error : Folder with specified name already exists
Code : 800403E9
Source : hMailServer COM library
The script with my customization (folder name only) :

Code: Select all

' DelOldSpam.vbs - Scott MacLean - v1.0 - 05/30/2008

Dim AdminUser
Dim AdminPass
Dim FromEmail
Dim ReportToEmail
Dim DaysOfSpam

AdminUser = "xxxxxxxxxxx"
AdminPass = "xxxxxxxxx"
FromEmail = "xxxxx@xxxxxxx.fr" ' Replace this with the email address you want the report to come "from"
ReportToEmail = "xxxxx@xxxxx.fr" ' Replace this with the email address you want the report to be sent to
DaysOfSpam = 7 ' Replace this with the number of days spam is kept before being deleted

Dim App
Dim Domains
Dim Domain
Dim Accounts
Dim Account
Dim IMAPFolders
Dim IMAPFolder
Dim Messages
Dim Message

Dim DomainNum
Dim AccountNum
Dim IMAPFolderNum
Dim MessageNum

Dim FoundSpamFolder

Dim OutputMsg

Dim NumMsgs
Dim AccountSize
Dim NumSpams
Dim NumDeleted
Dim TotalAccounts
Dim TotalNumMsgs
Dim TotalAccountSize
Dim TotalNumSpams
Dim TotalNumDeleted

Set App = CreateObject("hMailServer.Application")
Call App.Authenticate(AdminUser, AdminPass)

TotalNumMsgs = 0
TotalAccountSize = 0
TotalNumSpams = 0
TotalNumDeleted = 0

Set Domains = App.Domains

For DomainNum = 1 To Domains.Count
  Set Domain = Domains.Item(DomainNum - 1)
 
  If Domain.Active Then
 
    Set Accounts = Domain.Accounts
   
    NumMsgs = 0
    AccountSize = 0
    NumSpams = 0
    NumDeleted = 0
   
    For AccountNum = 1 To Accounts.Count
      Set Account = Accounts.Item(AccountNum - 1)
 
      If Account.Active Then
     
        Set IMAPFolders = Account.IMAPFolders
       
        FoundSpamFolder = False
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)
          If UCase(IMAPFolder.Name) = "Junk" Then
            FoundSpamFolder = True
            Exit For
          End If
        Next
       
        If Not FoundSpamFolder Then Set IMAPFolder = IMAPFolders.Add("Junk")
       
        For IMAPFolderNum = 1 To IMAPFolders.Count
          Set IMAPFolder = IMAPFolders.Item(IMAPFolderNum - 1)

          Set Messages = IMAPFolder.Messages

          If UCase(IMAPFolder.Name) = "Junk" Then
            NumSpams = NumSpams + Messages.Count
            TotalNumSpams = TotalNumSpams + Messages.Count
          End If

          NumMsgs = NumMsgs + Messages.Count
          TotalNumMsgs = TotalNumMsgs + Messages.Count
         
          For MessageNum = Messages.Count To 1 Step -1
            Set Message = Messages.Item(MessageNum - 1)

            AccountSize = AccountSize + Message.Size
            TotalAccountSize = TotalAccountSize + Message.Size
           
            If UCase(IMAPFolder.Name) = "Junk" Then

              If DateAdd("d", DaysOfSpam, Message.InternalDate) < Now Then
                NumDeleted = NumDeleted + 1
                TotalNumDeleted = TotalNumDeleted + 1
               
                Messages.DeleteByDBID Message.ID
              End If
            End If
          Next
        Next         
      End If
    Next
   
    TotalAccounts = TotalAccounts + Accounts.Count
   
    OutputMsg = OutputMsg & "<tr><td>" & Domain.Name & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(Accounts.Count, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AccountSize, 0, True, False, True) & "K</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumSpams, 0, True, False, True) & "</td>"
    OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf
   
  End If
Next

OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Domain</b></td><td><b>Users</b></td><td><b>Size</b></td><td><b>Messages</b></td><td><b>Spam</b></td><td><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg

OutputMsg = OutputMsg & "<tr><td><b>Total</b></td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccounts, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalAccountSize, 0, True, False, True) & "K</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumMsgs, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumSpams, 0, True, False, True) & "</td>"
OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(TotalNumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf

OutputMsg = OutputMsg & "</table>" & vbCrLf

Set Message = CreateObject("hMailServer.Message")

Message.FromAddress = FromEmail
Message.From = "Spam Deletion Daemon"
Message.AddRecipient "System Administrator", ReportToEmail
Message.Subject = "Daily spam deletion report"
Message.HTMLBody = OutputMsg
Message.Save
Thank you for your help.
JoN ;)

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-08-13 14:38

JoN: Sorry it took so long to reply, this is the first time in a while I've been on the forum and for whatever reason I didn't get a notice about the post on my threadwatch.

From the look of it, it's a simple typo

The UCase is looking for the folder name in all caps. It will never match "Junk".

Change the line that reads

If UCase(IMAPFolder.Name) = "Junk" Then

so it reads

If UCase(IMAPFolder.Name) = "JUNK" Then

amr123
Normal user
Normal user
Posts: 50
Joined: 2006-02-24 00:55
Contact:

Re: My script to delete spam email after x days old, with report

Post by amr123 » 2009-11-08 20:52

Hello Every one

Please , can any one tell me how i can use this to delete from sub folder

i use ASSP and i have global rule to move spam messages to Sub folder called spam under inbox

i tried all the versions here up and it fail with sub folder , it works OK with Main folders
i tried to write it INBOX.SPAM but no thing happens


thanks in advance

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: My script to delete spam email after x days old, with report

Post by ^DooM^ » 2009-11-08 20:58

Dunno whats wrong with the scrip but why not just create an ASSPSPAM folder off the root rather than off the INBOX instead and have assp put it there or have hMail move it there with a rule.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

pmw
Normal user
Normal user
Posts: 40
Joined: 2007-10-30 15:58

Re: My script to delete spam email after x days old, with report

Post by pmw » 2009-11-08 22:55

amr: DooM's suggestion would be the easiest idea. We use ASSP and have a rule that the stuff it marks as spam gets tossed into the spam folder (in the root). From there, this script wipes it away after so many days (in case ASSP made a mistake).

I am not sure how to go into sub-folders, though I could've sworn I found that one time.

I won't really have a chance to try any ideas till later in the week, but if you have some patience, I'll get back by Friday night with any suggestions. Or worst case I'll apologize for not having time to play with it. Things get busy this time of year for me.

Post Reply