Script to delete messages older than X days

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
adriano
New user
New user
Posts: 8
Joined: 2005-08-28 00:10
Location: Sunnyvale, CA
Contact:

Script to delete messages older than X days

Post by adriano » 2009-06-13 02:39

Here's an adaptation of various scripts and snippets found in the hMailServer 4 scripts forum. This works in hMailServer 5 (tested in 5.1.1).

It will delete all messages older than X days from a specific combination of domain/mailbox/folder -- can come in handy if you need to keep under control mailboxes which get lots of automated messages (such as bccs, logs, etc.).

How to use:

- copy what in the "code" section and save it for example as myscript.vbs
- invoke it from the command line with cscript myscript.vbs

Figured to share since I found a number of scripts deleting across domains / multiple mailboxes / etc. but nothing specific to one mailbox only.

- Adriano

Code: Select all

Const DAYS_TO_KEEP_MESSAGES = "3"	'' change as appropriate
Const MESSAGES_FOLDER = "INBOX"  	'' this will delete from the inbox folder, change as appropriate
Const DOMAIN_NAME = "yourdomainname.com"
Const ACCOUNT_NAME = "address@yourdomainame.com"

Const HMSADMINUSER = "hmsadministrator"
Const HMSADMINPWD = "hmsadministratorspassword"

Dim NumDeleted

On Error Resume Next

Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

Dim oDomain
Set oDomain = oApp.Domains.ItemByName(DOMAIN_NAME)  

Dim oAccount
Set oAccount = oDomain.Accounts.ItemByAddress(ACCOUNT_NAME)
	
Dim oMessages
Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages

Dim iMessages

'WScript.Echo oMessages.Count

NumDeleted = 0
For iMessages = 0 To oMessages.Count - 1

  Dim oMessage
  Set oMessage = oMessages.Item(iMessages)
 
  Dim vbDate
  vbDate = CDate(Mid(oMessage.Date, 6, 20))
 
  If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
	NumDeleted = NumDeleted + 1
	oMessages.DeleteByDBID(oMessage.ID)
  End If
Next

'WScript.Echo NumDeleted
Currently playing with hMailServer:
- 5.3-B1617 (w/ built-in MySQL)
- 5.3.3-B1879 (w/ built-in MySQL)
- 5.3.3-B1879 (w/ built-in MSSQL CE)

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

Re: Script to delete messages older than X days

Post by ^DooM^ » 2009-06-13 11:47

Nice one, thanks for sharing :)
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Vanden
New user
New user
Posts: 1
Joined: 2009-07-02 11:25

Re: Script to delete messages older than X days

Post by Vanden » 2009-07-02 11:34

Excellent script.

Can this script be easily configured to purge all accounts in the domain instead of single accounts.

Good work.

Thanks

Mark

Kwyjibo
New user
New user
Posts: 3
Joined: 2009-03-05 14:46

Re: Script to delete messages older than X days

Post by Kwyjibo » 2009-07-13 16:15

I've tweaked the code slightly to access all accounts on all domains. Thought someone may find it useful.

Code: Select all

Const DAYS_TO_KEEP_MESSAGES = "7"   '' change as appropriate
Const MESSAGES_FOLDER = "Junk"     '' this will delete from the inbox folder, change as appropriate

Const HMSADMINUSER = "HMAIL_ADMINSTRATOR"
Const HMSADMINPWD = "PASSWORD"

Dim NumDeleted

'On Error Resume Next

Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  Dim oDomain
  Set oDomain = oApp.Domains.Item(x)  
   
  For y = 0 to oDomain.Accounts.Count - 1
    Dim oAccount
	Set oAccount = oDomain.Accounts.Item(y)
	
    Dim oMessages
    Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages

    Dim iMessages

    NumDeleted = 0
    For iMessages = 0 To oMessages.Count - 1

      Dim oMessage
      Set oMessage = oMessages.Item(iMessages)

      Dim vbDate
      vbDate = CDate(Mid(oMessage.Date, 6, 20))

      If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
        NumDeleted = NumDeleted + 1
        oMessages.DeleteByDBID(oMessage.ID)
      End If
    Next
	'WScript.Echo "Removed " & NumDeleted & " message(s) from account " & oAccount.Address
  Next
Next 

Te-Ha
New user
New user
Posts: 3
Joined: 2009-08-03 08:39

Re: Script to delete messages older than X days

Post by Te-Ha » 2009-08-03 09:13

Hi All,

thank you very much for your efforts in scripting!!! maybe I am too dumb to use it correctly, but I have problems with this script: is it meant to clean the DB and the messagefiles? I realized that message files will be deleted - but not all of them.

I use hMailServer V 5.1.2.

Say I have a domain called: mydom.com, I want to delete messages older than 1 day from the INBOX of myaccount@mydomain.com
DB is MySQL called hmail and user administrator with password mypass (user from Admin GUI)

Const DAYS_TO_KEEP_MESSAGES = "1"
Const MESSAGES_FOLDER = "INBOX"
Const DOMAIN_NAME = "mydom.com"
Const ACCOUNT_NAME = "myaccount@mydom.com"
Const HMSADMINUSER = "Administrator"
Const HMSADMINPWD = "mypass"

Every day there is a total of about 7000 messages being sent to the account (from a separate SPAM-Gateway). The script now runs for 6 days (as a scheduled task). It deletes messages, I find ca. 6000 files in total in the user's INBOX (if none of them was deleted I should find > 35000).

****************
Edit: I "echoed" the output
- WScript.Echo "Mails before: " & oMessages.Count
- WScript.Echo "Mails deleted: " & NumDeleted
from the calling BAT to a TXT-file : cscript myscript.vbs > myascii.txt

This confirms the deletion.
End of Edit
****************

The strange thing is that I still have lots of messages older than 1 day!

What is it I'm doing wrong?

Thank you very much for your help.

Regards,
Thomas

Te-Ha
New user
New user
Posts: 3
Joined: 2009-08-03 08:39

Re: Script to delete messages older than X days

Post by Te-Ha » 2009-08-05 11:07

Hi,

after taking a closer look at the script and my specific mail environment I can say: the script works like it should.

Sorry for doubting. :oops:

My problem is/was that mails will arrive in peaks (sent from a separate spam gateway) so there is no constant time/amount ratio, which makes it difficult to evaluate. On the other hand the amount of spam is rather big.

Regards,
Thomas

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

Re: Script to delete messages older than X days

Post by bigworm » 2009-09-15 16:31

This did not appear to work fully...I ran it on a mailbox with ~2,000 messages and gave it a retention time of 260 days and I still had messages well back into early 2008. Any ideas?

Thanks!
hMailServer 5.4 - Build 1946 + ASSP v2.4.3(14246)

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

Re: Script to delete messages older than X days

Post by bigworm » 2009-09-15 17:09

Well, I ran it again, and again...each time it deleted a couple hundred more messages...is this normal or ?
hMailServer 5.4 - Build 1946 + ASSP v2.4.3(14246)

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

Re: Script to delete messages older than X days

Post by amr123 » 2009-11-08 19:11

Hello every body

i have a problem with this script , i use the modification that "Kwyjibo" made on Hmail Vesion 5.3 on Windows server 2003 . i saved the script on c\delete.vbs then i run it with cscript

in my setup each account has a subfolder from the inbox named sapm , so i changed the folder messages as bellow
Const MESSAGES_FOLDER = "INBOX.Spam"

i got a error meessage on the dos prompat says
" C:\Delete.vbs(24, 5) Microsoft VBScript runtime error: Subscript out of range"

i tried to uncomment the line of 'On Error Resume Next then re-run it , the echo message tells that it deleted one message from each account


any ideas ?
i appreciate you help please


thanks , Amr

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

Re: Script to delete messages older than X days

Post by mattg » 2009-11-08 23:15

run cscript in debug mode. https://www.microsoft.com/windows/windo ... prompt.htm

That may you give you some more hints...

Which is line 24 of your script?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Te-Ha
New user
New user
Posts: 3
Joined: 2009-08-03 08:39

Regional settings? Re: Script to delete messages older than

Post by Te-Ha » 2009-11-16 10:21

Hi,

might it be that different regional settings (other than English) might prevent the script from running in my (!!!) case? Or is it the way VB determines the date, or a combination of both?

I had problems executing the script in months where there is a difference in spelling, so the ; I use a XP German, localized to GER version.
Example:
The 10th month of the year, problems with the script :mrgreen: :
- German: Oktober --> OKT
- English: October --> OCT
The 11th month :mrgreen:
- German: November --> NOV
- English: November --> NOV

I had problems in MAI / MAY, but no problems in JUN, JUL, AUG, SEP.

Regards,
Thomas

fjehoel
New user
New user
Posts: 6
Joined: 2009-12-04 11:36

Re: Script to delete messages older than X days

Post by fjehoel » 2009-12-24 17:56

I made some changes to the code that should remove the problems described by amr123 and bigworm.

Because the number of messages decreases when a message is deleted, the loop over the messages didn't work correctly. If e.g. message 5 of 10 is deleted it moves to the next message which is number 6. But because the total is now messages left is now 9, this message 6 of 9 is actually message 7 of 10, so in fact you skip message 6 of 10 (which would be the fifth after you delete number 5). To correct this, I do the loop backwards so that every message is checked. I also added a check to see if the date of the message checks out. If it doesn't, I delete it. Since I'm using this to delete old junk mail this is ok, but if you use it on a folder with more important mail you may want to remove those lines. I would advise havind some sort of check on the date, because the CDate function will give an error if something is wrong with the date.

Code: Select all

Const DAYS_TO_KEEP_MESSAGES = "7"   '' change as appropriate
Const MESSAGES_FOLDER = "Junk E-mail"     '' this will delete from the junk folder, change as appropriate

Const HMSADMINUSER = "Administrator"
Const HMSADMINPWD = "password"

Dim NumDeleted

'On Error Resume Next

Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  Dim oDomain
  Set oDomain = oApp.Domains.Item(x)  
   
  For y = 0 to oDomain.Accounts.Count - 1
    Dim oAccount
    Set oAccount = oDomain.Accounts.Item(y)
   
    Dim oMessages
    Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages

    Dim iMessages

    NumDeleted = 0
	
    Dim numMessages
    numMessages = oMessages.Count
    
    For iMessages = 0 To numMessages - 1

      Dim oMessage
      Set oMessage = oMessages.Item(numMessages - 1 - iMessages)
      'Wscript.Echo "oMessage " & iMessages & " of " & numMessages & "Date " & Mid(oMessage.Date, 6, 20)

      Dim vbDate
      If (IsDate(Mid(oMessage.Date, 6, 20)) = False) Then
        NumDeleted = NumDeleted + 1
        oMessages.DeleteByDBID(oMessage.ID)
      Elseif (CDate(Mid(oMessage.Date, 6, 20)) < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
        NumDeleted = NumDeleted + 1
        oMessages.DeleteByDBID(oMessage.ID)
      End If
    Next
  WScript.Echo "Removed " & NumDeleted & " of " & numMessages & " message(s) from account " & oAccount.Address
  Next
Next

henlon
New user
New user
Posts: 9
Joined: 2007-05-15 23:34
Location: Sweden

Re: Script to delete messages older than X days

Post by henlon » 2010-01-31 14:01

After several accounts I get an error in raw 24, saying "wrong index in matrix"

What will happend if the spam folder doesent exist in an account?

ozhim
New user
New user
Posts: 2
Joined: 2009-12-20 13:10
Location: TR
Contact:

Re: Script to delete messages older than X days

Post by ozhim » 2010-02-19 20:33

all domain and user inbox in mail,
want last 7 days before incoming mail delete

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

Re: Script to delete messages older than X days

Post by ^DooM^ » 2010-02-19 22:06

Feel free to modify these scripts to do what you need.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

ticktock666
New user
New user
Posts: 3
Joined: 2010-01-09 18:55

Re: Script to delete messages older than X days

Post by ticktock666 » 2010-02-23 19:42

Great work, I've been looking for a way to do this.

I have hmailserver with 1 domain and 11 users, I just need to delete emails older than 10 days for every user, I've tried using fjehoel script but it's giving me an error message :

when Running .vbs file from CMD (cscript command)

Code: Select all

C:\mailclean.vbs(15, 1) hMailServer COM library: You do not have access to this
property / method. Ensure that hMailServer.Application.Authenticate() is called
with proper login credentials.
when double clicking the .vbs file :

Code: Select all

---------------------------
Windows Script Host
---------------------------
Script:	C:\mailclean.vbs
Line:	15
Char:	1
Error:	You do not have access to this property / method. Ensure that hMailServer.Application.Authenticate() is called with proper login credentials.
Code:	800403E9
Source: 	hMailServer COM library

---------------------------
OK   
---------------------------
any pointers to what the problem might be would be appreciated.

Also, I thought about manually deleting the emails, since there aren't that many, by removing the folders/emails from /DATA/domain/user/, but when connecting to the server it started delivering all the emails again (no subject and all with the same time tag), so I assumed I need to do something with the database to remove the missing emails, only to find it's something I've never encountered before (SQL Compact Edition).

I've tried to follow the guide here : http://www.hmailserver.com/documentatio ... pair_mysql
to correct the errors but the SQLyog reports that it can't find any databases to connect to.

DeanoX
Senior user
Senior user
Posts: 480
Joined: 2005-11-05 00:07
Location: Michigan

Re: Script to delete messages older than X days

Post by DeanoX » 2010-02-23 19:47

Post your script.

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

Re: Script to delete messages older than X days

Post by mattg » 2010-02-24 02:01

ticktock666 wrote:C:\mailclean.vbs(15, 1) hMailServer COM library: You do not have access to this
property / method. Ensure that hMailServer.Application.Authenticate() is called
with proper login credentials.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

Kwyjibo
New user
New user
Posts: 3
Joined: 2009-03-05 14:46

Re: Script to delete messages older than X days

Post by Kwyjibo » 2010-02-27 22:43

Hi all, I've updated my version of the script to take into consideration the following:
  • Deleting a message altered the indexes of remaining messages - now counts backwards
  • Didn't always delete a message older than x days as mail header date used - now uses hMailServer internal date
The on error should no longer be needed. Uncomment the Echo lines to check it is working.

Code: Select all

Const DAYS_TO_KEEP_MESSAGES = "7"   ' change as appropriate
Const MESSAGES_FOLDER = "Junk"     ' this will delete from the Junk folder, change as appropriate

Const HMSADMINUSER = "HMAIL_ADMIN"   ' change as appropriate
Const HMSADMINPWD = "PASSWORD"   ' change as appropriate

Dim NumDeleted

'On Error Resume Next

Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  Dim oDomain
  Set oDomain = oApp.Domains.Item(x)  
   
  For y = 0 to oDomain.Accounts.Count - 1
    Dim oAccount
    Set oAccount = oDomain.Accounts.Item(y)
	
    Dim oMessages
    Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages

    Dim iMessages

    NumDeleted = 0
	
    'WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & " messages"
	
    If oMessages.Count > 0 Then
    		
      For iMessages = (oMessages.Count - 1) To 0 step -1

        Dim oMessage
        Set oMessage = oMessages.Item(iMessages)

        Dim vbDate
				
        vbDate = oMessage.InternalDate
			
        If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
          NumDeleted = NumDeleted + 1
          oMessages.DeleteByDBID(oMessage.ID)
        End If
      Next
    End If
	
    'WScript.Echo "Removed " & NumDeleted & " message(s) from account " & oAccount.Address
  
  Next
Next 
Don't forget this will delete emails from all accounts in all domains in the folder specified older than the number of days specified

markylondon
New user
New user
Posts: 22
Joined: 2010-03-08 15:57

Re: Script to delete messages older than X days

Post by markylondon » 2010-03-08 16:51

Thanks for this script, works perfect for me :)

JamesH
New user
New user
Posts: 25
Joined: 2008-05-14 12:21

Re: Script to delete messages older than X days

Post by JamesH » 2010-04-29 21:04

Is it possible to easily modify this script to just move emails older than x days to an IMAP folder named say 'Archive' in all accounts?

Can this be easily achieved? Any advice on where to start with the script?

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

Re: Script to delete messages older than X days

Post by mattg » 2010-04-30 03:18

Yes should be easy enough to achieve...

Before this line - oMessages.DeleteByDBID(oMessage.ID)

insert some lines of code to check that the 'archive' folder exists, if not create it, and then copy the message to the 'archive' IMAP folder, and delete the original message (with the above line)
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

cnt
Normal user
Normal user
Posts: 34
Joined: 2007-09-14 22:11

Re: Script to delete messages older than X days

Post by cnt » 2010-08-01 12:15

Hello,

I was trying to install the corrected script as Kwyjibo published above (2010-03-08 15:5) like this:
I edited only name and password. However did not know how to deal with the line "Const MESSAGES_FOLDER = "Junk", I think I do not have these folders since except one account nobody else is using IMAP. :|


I put entire script to EventHandlers.vbs in Event folder of Hmail - between uncommented lines:
Sub OnBackupCompleted() .... End Sub

I reloaded the script, syntax was ok, then performed backup of Hmail and nothing happened.
So what now?

PS I do not program in vb

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

Re: Script to delete messages older than X days

Post by mattg » 2010-08-02 08:58

This would be run external to your eventhandlers.vbs

You should just open notebook, paste the text to it and save as 'anything.vbs' somewhere in your file system. (You could run this as a scheduled task every week if you wanted).

Everyone has an 'inbox', and that is all that POP3 accounts will have. Are you sure that you want to delete all messages > 7 days old in everyone's inbox. That may remove some messages that haven't been read yet.

Be careful!!
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

cnt
Normal user
Normal user
Posts: 34
Joined: 2007-09-14 22:11

Re: Script to delete messages older than X days

Post by cnt » 2010-08-02 10:35

sorry for been new in this:

ok, I left file EventHandlers.vbs empty (just like after the installation).
I created the file delete.vbs and placed it in the root of hmail program
I created task "C:\Program Files\hMailServer\delete.vbs" (from "C:\Program Files\hMailServer")
I pasted the code from here above - Posted: 2010-02-27 21:43 by Kwyjibo: edited only admin, psswd, days and instead of word "Junk" on the second line I put the word "inbox".

I tried to run task but nothing happened.
Const MESSAGES_FOLDER = "Junk" // I do not even know, what is the name of my inbox. I put this:

Const MESSAGES_FOLDER = "inbox" // but is it correct?

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

Re: Script to delete messages older than X days

Post by mattg » 2010-08-02 15:21

A few posts up I showed how to debug a script...
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

cnt
Normal user
Normal user
Posts: 34
Joined: 2007-09-14 22:11

Re: Script to delete messages older than X days

Post by cnt » 2010-08-02 21:21

I have
Const DAYS_TO_KEEP_MESSAGES = "100"
Const MESSAGES_FOLDER = "inbox"

and in log file this after runing the script:

"DEBUG" 1712 "2010-08-02 20:28:43.375" "Deleting message file"
"DEBUG" 1712 "2010-08-02 20:28:43.375" "Ending session 31016"
"DEBUG" 3512 "2010-08-02 20:28:44.922" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.922" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.922" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.922" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:44.937" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.265" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:45.406" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.515" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.515" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.719" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.719" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.719" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.719" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.719" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:45.797" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:45.890" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.094" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.172" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.172" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.172" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 2932 "2010-08-02 20:28:46.297" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.359" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.406" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.406" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.515" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.578" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.578" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.578" "Reading message from database"
"DEBUG" 3512 "2010-08-02 20:28:46.578" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.719" "Reading message from database"
"DEBUG" 1828 "2010-08-02 20:28:46.719" "Reading message from database"

the file was here:

"C:\Program Files\hMailServer\delete.vbs"

Result: no deletion.

ObiWan
Senior user
Senior user
Posts: 280
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Script to delete messages older than X days

Post by ObiWan » 2010-08-03 16:17

Kwyjibo wrote:

Code: Select all

...
        If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
          NumDeleted = NumDeleted + 1
          oMessages.DeleteByDBID(oMessage.ID)
        End If
Uhm... what about using DateDiff() there :) ?

I mean, something like

Code: Select all

Function IsMsgExpired(dtMail, nMaxDays)
  Dim nDays
  
  On Error Resume Next
  IsMsgExpired = False
  nDays = DateDiff("d", dtMail, Now())
  If (nDays > nMaxDays) Then
    IsMsgExpired = True
  End If
End Function
at that point you may call the function and if it returns "true"
proceed to (archive and then) delete the message; also and
since we're at it, the script may also deal with "archives" so
that; once completed the cleanup, it may check archived emails
and totally remove the really obsolete ones

As for archiving, you may use something like this to create "zip"
archives naming them (e.g.) "yyyymm-mailbox.zip" and add old
emails to such archives; then it will be quite simple to generate
a list of the "zip" archive files sitting inside the archive folder and
remove the ones older than "n" :)

(note: the "mailbox" used for the name of the "zip" will be the actual email box name so that it will be quite straightforward recovering a given archive)

ObiWan
Senior user
Senior user
Posts: 280
Joined: 2010-07-21 14:30
Location: Halfway between Germany and Egypt

Re: Script to delete messages older than X days

Post by ObiWan » 2010-08-05 18:19

ObiWan wrote: As for archiving, you may use something like this to create "zip"
archives naming them (e.g.) "yyyymm-mailbox.zip" and add old
emails to such archives
Just in case; here's a simple code snippet

Code: Select all


' adds a file to a zip archive whose name will be
' created in the given path using the year/month
' for its name
Function ArchiveFile(sArcPath, dtDateTime, sFile)
  Dim objZip, dtTemp, sZipFile

  On Error Resume next
  
  ' create the "zip" object
  Set objZip = CreateObject("XStandard.Zip")
  If Err.Number <> 0 Then
    ArchiveFile = False
    Exit Function
  End If
  
  ' generate the zip filename by using the
  ' given path and the year/month; resulting
  ' file name will be "path\yyyy-mm-mailbox.zip"
  dtTemp = CDate(dtDateTime)
  sZipFile = sArcPath & "\" & _
             Right("0000" & Year(dtTemp), 4) & "-" & _
             Right("00" & Month(dtTemp), 2) & _
             "-mailbox.zip"

  ' now add the given file to the zip
  With objZip
    .Pack sFile, sZipFile, False, , 9
    If .ErrorCode <> 0 Then
      ArchiveFile = False
      Exit Function
    End If
  End With
  
  ' all done
  ArchiveFile = True
End Function

you may just call the above function passing the path in
which you want to store the zip files, the message date
and the message file pathname; the function will then
add the given file to the zip and return "true" if all ok,
at that point you may go on deleting the message file
and still having the old messages stored into "zips"
which may then, from time to time, be easily deleted
for example, you may then delete zips older than
(say) 3 months

DeMeTec
New user
New user
Posts: 2
Joined: 2010-07-10 13:33

Re: Script to delete messages older than X days

Post by DeMeTec » 2010-09-16 20:04

hi!

I have problems deleting mail from IMAP folders using your Scripts, kwyjibo and fjehoel.

Both of your scripts produce
killoldspam.vbs(24, 5) Laufzeitfehler in Microsoft VBScript: Index außerhalb des gültigen Bereichs
(runtime error: index out of valid range)

Note: <A> to <D> stand for regular email adresses.
The Central public folder has the path #Zentral/Antispam/spam


fjehoel's script states:
Removed 0 of message(s) from account <A>
Removed 0 of message(s) from account <B>
Removed 0 of message(s) from account <C>
Removed 0 of message(s) from account <D>

kwyjibo's script states:
Removed 1 message(s) from account <A>
Removed 1 message(s) from account <B>
Removed 1 message(s) from account <C>
Removed 1 message(s) from account <D>
however, it does not remove any mail at all. When I run it again, it again says it deletes 1 message.

What modifications need to be done to use the script on the central Imap folder?

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-18 22:19

I fought with several of these scripts and none seemed to work.

This DOS script will delete all email in any domain whos age is greater than 5 days (adjustable) and are in the 'Trash' folder (adjustable).

I'm not a VB guy, so I wrote my own batch file. Requires admin access on the windows box, or at least access to the mysql.exe, read/write/delete permission in the directory you run it in and the %hmailserver%\data

Code: Select all

@echo off
cls

rem ------------
rem MYSQL vars
rem ------------

set mysqlexe="D:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
set mysqluser=CHANGEME
set mysqlpass=CHANGEME
set mysqldb=hmail

rem ------------
rem Hmail Vars
rem ------------

set trashfolder=Trash
set ageindays=5

rem ------------------------------------------------------------------------
rem Do not modify below this line
rem ------------------------------------------------------------------------

rem ' Main, squish sql controls
set mysql=%mysqlexe% -u%mysqluser% -p%mysqlpass% --database %mysqldb%
rem ' Main, empty var space
set mysqlexe=
set mysqluser=
set mysqlpass=
set mysqldb=

rem ' Main, create sql script to collect file names and IDs of contents 
rem - the following is one line, and should not be word-wraped
echo SELECT hm_messages.messageid, hm_messages.messagefilename, hm_messages.messagefolderid FROM hm_messages,hm_imapfolders WHERE hm_messages.messagecreatetime ^<= date_sub(now(), interval %ageindays% day) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%trashfolder%';>oldmail.sql

rem ' Main, get trashfolder contents
%mysql%<oldmail.sql>oldmail.txt

rem ' Main, create del sql script
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do echo DELETE FROM hm_messages WHERE messageid = '%%a'; >>del.sql

rem ' Main, delete sql entries
%mysql%<del.sql

rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y

rem Cleanup
del oldmail.txt
del oldmail.sql
del del.sql

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

Re: Script to delete messages older than X days

Post by mattg » 2010-11-19 03:46

Good work and thanks for posting, but that won't actually delete the mail message from the file system, only remove the metadata entries from the database about these messages. Is that correct?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

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

Re: Script to delete messages older than X days

Post by ^DooM^ » 2010-11-19 10:01

Nope he does it here reading the file name from oldmail.txt

Code: Select all

rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-19 16:27

^DooM^ wrote:Nope he does it here reading the file name from oldmail.txt

Code: Select all

rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y
correct.

I was going to make a few more scripts to either
prune all messages from all folders older than 30 days
prune all messages older than 30 days from all folders except a 'keepme' folder
prune 1 mailbox of all mail older than 2 days (mirror mailbox for mailarchiva)

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-19 16:44

actually to delete all mail older than xx

change

Code: Select all

echo SELECT hm_messages.messageid, hm_messages.messagefilename, hm_messages.messagefolderid FROM hm_messages,hm_imapfolders WHERE hm_messages.messagecreatetime ^<= date_sub(now(), interval %ageindays% day) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%trashfolder%';>oldmail.sql
to

Code: Select all

echo SELECT messageid, messagefilename FROM messages where messagecreatetime <= date_sub(now(), interval %ageindays% day) ;>oldmail.sql
it will also ignore the folder name, and process everything.

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-19 17:15

similarly, to delete all mail older than xx days in any folder other than a 'keep me' folder:

Code: Select all

@echo off
cls

rem ------------
rem MYSQL vars
rem ------------

set mysqlexe="D:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
set mysqluser=CHANGEME
set mysqlpass=CHANGEME
set mysqldb=hmail

rem ------------
rem Hmail Vars
rem ------------

set keepfolder=Keep
set ageindays=5

rem ------------------------------------------------------------------------
rem Do not modify below this line
rem ------------------------------------------------------------------------

rem ' Main, squish sql controls
set mysql=%mysqlexe% -u%mysqluser% -p%mysqlpass% --database %mysqldb%
rem ' Main, empty var space
set mysqlexe=
set mysqluser=
set mysqlpass=
set mysqldb=

rem ' Main, create sql script to collect file names and IDs of contents 
rem - the following is one line, and should not be word-wraped
echo SELECT hm_messages.messageid, hm_messages.messagefilename, hm_messages.messagefolderid FROM hm_messages,hm_imapfolders WHERE hm_messages.messagecreatetime ^<= date_sub(now(), interval %ageindays% day) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername != '%keepfolder%';>oldmail.sql

rem ' Main, get mail contents
%mysql%<oldmail.sql>oldmail.txt

rem ' Main, create del sql script
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do echo DELETE FROM hm_messages WHERE messageid = '%%a'; >>del.sql

rem ' Main, delete sql entries
%mysql%<del.sql

rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y

rem Cleanup
del oldmail.txt
del oldmail.sql
del del.sql

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-19 18:15

This one will delete mail in <TARGETFOLDER> e.g. Trash (adjustable) ... older than 5 (adjustable) but only in TARGETDOMAIN (adjustable)

Code: Select all


@echo off
cls

rem ------------
rem MYSQL vars
rem ------------

set mysqlexe="D:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
set mysqluser=CHANGEME
set mysqlpass=CHANGEME
set mysqldb=hmail

rem ------------
rem Hmail Vars
rem ------------

set targetfolder=Trash
set targetdomain=mydomain.com
set ageindays=5

rem ------------------------------------------------------------------------
rem Do not modify below this line
rem ------------------------------------------------------------------------

rem ' Main, squish sql controls
set mysql=%mysqlexe% -u%mysqluser% -p%mysqlpass% --database %mysqldb%
rem ' Main, empty var space
set mysqlexe=
set mysqluser=
set mysqlpass=
set mysqldb=

rem ' Main, create sql script to collect file names and IDs of contents 
rem - the following is one line, and should not be word-wraped
echo SELECT messageid,messagefilename FROM hm_messages,hm_accounts,hm_domains,hm_imapfolders WHERE hm_messages.messageaccountid = hm_accounts.accountid AND hm_accounts.accountdomainid = hm_domains.domainid AND hm_domains.domainname = '%targetdomain%' AND hm_messages.messagecreatetime ^<= date_sub(now(), INTERVAL %ageindays% DAY) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%targetfolder%';>oldmail.sql

rem ' Main, get target contents
%mysql%<oldmail.sql>oldmail.txt

rem ' Main, create del sql script
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do echo DELETE FROM hm_messages WHERE messageid = '%%a'; >>del.sql

rem ' Main, delete sql entries
%mysql%<del.sql

rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y

rem Cleanup
del oldmail.txt
del oldmail.sql
del del.sql
Also Theres an error in one of my posts a couple back:
shentrax wrote:actually to delete all mail older than xx

change

Code: Select all

echo SELECT hm_messages.messageid, hm_messages.messagefilename, hm_messages.messagefolderid FROM hm_messages,hm_imapfolders WHERE hm_messages.messagecreatetime ^<= date_sub(now(), interval %ageindays% day) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%trashfolder%';>oldmail.sql
to

Code: Select all

echo SELECT messageid, messagefilename FROM messages where messagecreatetime <= date_sub(now(), interval %ageindays% day) ;>oldmail.sql
it will also ignore the folder name, and process everything.
forgot an upcarrot; should be:

Code: Select all

echo SELECT messageid, messagefilename FROM messages where messagecreatetime ^<= date_sub(now(), interval %ageindays% day) ;>oldmail.sql

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-11-30 19:06

Ok. This is a Inform/Delete script.

This script has some more variables; which will allow you to either Warn or just Inform the user of the impending deletion. E.G. You want to tell the user that 'target' emails are deleted or will be deleted.

It only reads partial subjects lines, and will grab the first FROM(reply-to), TO, DATE and SUBJECT found in an email.

NOTE: this requires a command line emailer, I use V-Mailer (google vmailer) cause it uses authentication
NOTE2: requires read/write/delete/execute to its own folder and %hmailserver%\data
NOTE3: It may have problems when email accounts exceed two 'dots' john.smith.doe@domain.com; I've only tested addresses with one 'dot' or no 'dot' john.doe@domain.com or john@domain.com

Variables include:
targetdomain = Target Domain
targetfolder = Target Folder
ageindays = Process email as old or older than xx days

warnonly = 'yes' will only inform the user, 'no' will also delete the mails.

useauth = Wether or not your outbound server requires authentication
mailaddress = the account from the mail will come from; e.g. system@domain.com
mailersmtp = smtp server to use; default is localhost
mailerpass = if you use authentication, you'll need to provide a password; if not, leave empty.

Code: Select all

@echo off
cls

rem ------------
rem MYSQL Vars
rem ------------

set mysqlexe="D:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
set mysqluser=CHANGEME
set mysqlpass=CHANGEME
set mysqldb=hmail

rem ------------
rem Hmail Vars
rem ------------

set targetfolder=Trash
set targetdomain=CHANGEFORTARGETDOMAIN.com
set ageindays=5

rem ------------
rem Script Vars
rem ------------
rem Warn Only - 'yes' will only generate email summaries, while setting this to 'no' will also Delete the mail
rem using 'yes' is helpful if you want to notify but not delete, e.g. delete job runs later/next day.
rem if you use a delete script with this, like this runs Tuesday and the delete scrip runs on Wednesday
rem it is suggested to adjust the delete script to include an extra day-of-age like +1 to the ageindays of
rem of this script.
set warnonly=yes

set useauthentication=yes
set maileraddress=system@domain.com
set mailersmtp=localhost
set mailerpass=SMTPPASSWORD

rem ------------------------------------------------------------------------
rem Do not modify below this line
rem ------------------------------------------------------------------------

rem ' Main, squish sql controls
set mysql=%mysqlexe% -u%mysqluser% -p%mysqlpass% --database %mysqldb%
rem ' Main, empty var space
set mysqlexe=
set mysqluser=
set mysqlpass=
set mysqldb=
del out.*

rem ' Main, create sql script to collect file names and IDs of contents 
rem - the following is one line, and should not be word-wraped
echo SELECT messageid,messagefilename FROM hm_messages,hm_accounts,hm_domains,hm_imapfolders WHERE hm_messages.messageaccountid = hm_accounts.accountid AND hm_accounts.accountdomainid = hm_domains.domainid AND hm_domains.domainname = '%targetdomain%' AND hm_messages.messagecreatetime ^<= date_sub(now(), INTERVAL %ageindays% DAY) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%targetfolder%';>oldmail.sql

rem ' Main, get target contents
%mysql%<oldmail.sql>oldmail.txt

rem ' Main, create del sql script
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do echo DELETE FROM hm_messages WHERE messageid = '%%a'; >>del.sql

rem ' Main, delete sql entries
if /i "%warnonly%"=="no" %mysql%<del.sql

rem ' Main, Warn Only?
if /i "%warnonly%"=="no" goto:delete
goto:skipdelete
:delete
rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y

:skipdelete
rem ' Main, processing SQL dump file for file names
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do set file="%%b"&call:read

goto:send

:read
echo -=-=-=-=-=-=-=-=-=-=- NEW MESSAGE -=-=-=-=-=-=-=-=-=-=-=-
rem ' Read, Clear vars between files
set d=
set f=
set s=
set sub=
set from=
set mdate=
set to=
set msgfile=
set dumped=
rem ' Read, Strip extra slashed in file names
set file=%file:\\=\%
rem ' Read, Read message files and collect data
for /f "tokens=1,2,3,4,5,6,7,8 delims=: " %%a in ('type %file%') do set a="%%a"&set b="%%b"&set c="%%c"&set dh="%%c %%d %%e %%f:%%g:%%h"&set bc="%%b %%c"&call:msg
goto:eof

:msg
if "%dumped%"=="mark" goto:eof
rem ' Read, Strip extra quotes from passing vars
set a="%a:"=%"
set b="%b:"=%"

rem ' Read, Process line items for Date, From and Subject

:msg1
rem ' Read, FROM
if "%from%"=="mark" goto:msg2
if /i %a%=="Return-Path" set f=%b%&set from=mark&echo from = %b%&goto:eof

:msg2
rem ' Read, SUBJECT
if "%sub%"=="mark" goto:msg3
if /i %a%=="subject" set s=%bc%&set sub=mark&echo subject = %bc%&goto:eof

:msg3
rem ' Read, DATE
if "%mdate%"=="mark" goto:msg4
if /i %a%=="date" set d=%dh%&set mdate=mark&echo mdate = %dh%&goto:eof

:msg4
if "%sub%"=="" goto:eof
if "%from%"=="" goto:eof
if "%mdate%"=="" goto:eof

rem ' Read, Dump message data to out file
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%d"=="data" set to="%%f@%%e"&set msgfile=%%f.%%e
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%c"=="data" set to="%%e@%%d"&set msgfile=%%e.%%d
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%b"=="data" set to="%%d@%%c"&set msgfile=%%d.%%c
echo to = %to%
if "%mdate%"=="mark" echo %d%,%f%,%s%>>out.%to%
set dumped=mark
goto:eof

:send
rem ' Send

:vmail
for /f "tokens=1* delims=." %%a in ('dir /b out.*') do set to="%%b"&call:draft
goto:end

:draft
rem ---------------------------------------------------
rem ' Draft Email that will be sent to recipients.
rem ---------------------------------------------------
echo FROM: %maileraddress%>out.eml
echo TO: %to%>>out.eml
if /i "%warnonly%"=="yes" echo SUBJECT: Target Email Will Be Deleted.>>out.eml
if /i "%warnonly%"=="no" echo SUBJECT: Target Email Have Been Deleted.>>out.eml
echo.>>out.eml
if /i "%warnonly%"=="yes" echo Targeted emails will be deleted from your email account.>>out.eml
if /i "%warnonly%"=="no" echo Targeted emails have been deleted from your email account.>>out.eml
echo They exceed one or more system policies for %targetdomain%:>>out.eml
echo  - Email Older than %ageindays% days in the '%targetfolder%' folder.>>out.eml
echo -------------------------------------------------------------------->>out.eml
echo DATE......................FROM....................Subject (partial)>>out.eml
echo -------------------------------------------------------------------->>out.eml
type out.%to%>>out.eml

rem '' VMailer.exe (file) (server) (to) (from) (auth username-optional) (auth pass-optional)
if /i "%useauthentication%"=="yes" vmailer.exe out.eml %mailersmtp% %to% %maileraddress% %maileraddress% %mailerpass%
if /i "%useauthentication%"=="no" vmailer.exe out.eml %mailersmtp% %to% %maileraddress%
goto:eof

:end
rem ' Cleanup
del oldmail.txt
del oldmail.sql
del out.*
del del.sql
sample output is:

Targeted emails will be deleted from your email account.
They exceed one or more system policies for domain.com:
- Email older than 5 days in the 'Trash' folder.
--------------------------------------------------------------------
DATE......................FROM....................Subject (partial)
--------------------------------------------------------------------
"20 Nov 2010 16:08:42","user@domain.com","FW [SPAM] "

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-12-08 17:31

I'd like to add that any of my scripts should work for 5.3

There seems to be a change coming in 5.4 that will break them; so i'll tackle that once released.
  • hMailServer no longer stores the full path to message files in the database.

Bill48105
Developer
Developer
Posts: 6192
Joined: 2010-04-24 23:16
Location: Michigan, USA

Re: Script to delete messages older than X days

Post by Bill48105 » 2010-12-08 17:44

Nice work shentrax but personally I'd prefer doing it in vbscript using the COM interface like the original post but always good to have various ways to do things. As you see changes in hmail (full path thing) can bite you, I'd be concerned with directly working with EML files and the database vs using COM & letting hmail handle the timing/caching etc, and to me so much in DOS scripts feel just so hackish compared to vbsscript..

Anyhow, to each his own & I bet many people will find your work useful. Heck I might even use one of them in a pinch. :)
Bill
hMailServer build LIVE on my servers: 5.4-B2014050402
#hmailserver on FreeNode IRC https://webchat.freenode.net/?channels=#hmailserver
*** ABSENT FROM hMail! Those in IRC know how to find me if urgent. ***

shentrax
New user
New user
Posts: 16
Joined: 2010-11-18 22:09

Re: Script to delete messages older than X days

Post by shentrax » 2010-12-08 18:36

Personally I've always had trouble grasping VB scripting; though I've also not had any use for it in my profession. I've done tons of DOS batch work; which It can get rather lengthy and clumsy looking. But at least I know I can do do what I need to get done. For sure not the best way, not the prettiest way, and prolly not the easiest way.

But If someone finds them useful, and easy as a copy/paste and a task scheduler..... mission accomplished.

Wanted to contribute something; and its the best I can do. 8)

Bill48105
Developer
Developer
Posts: 6192
Joined: 2010-04-24 23:16
Location: Michigan, USA

Re: Script to delete messages older than X days

Post by Bill48105 » 2010-12-08 18:50

Oh I didn't say I was any good with vbscript! lol I too have a very hard time at times but get by. And because of that I've been guilty of using batch files at times when other methods might be 'better'. ;)

Definitely not knocking your work. Seems it'd do the trick. Just care needs to be taken in event of changes in hmail like you noted but also differences in database used (looks like yours is mysql only at this point) and what I perceive as higher potential for conflict/corruption than letting hmail do the low-level stuff by using COM interface provided. (The 1st post in this thread is a good example of that method aside from fact it assumes the folder exists for each user otherwise it pukes.. A check would have been a idea. lol)

Either way don't take my personal opinions or pointing out of potential issues personally, we each use what works for us and I think people will find what you posted useful. :)
Bill
hMailServer build LIVE on my servers: 5.4-B2014050402
#hmailserver on FreeNode IRC https://webchat.freenode.net/?channels=#hmailserver
*** ABSENT FROM hMail! Those in IRC know how to find me if urgent. ***

marceloa
Normal user
Normal user
Posts: 33
Joined: 2010-05-08 20:00

Re: Script to delete messages older than X days

Post by marceloa » 2011-02-16 04:15

the first script works for me well, but only the inbox directory, why not in SENT, i tried

Sent
sent
INBOX.Sent

and no way !

thanks
Marcelo

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

Re: Script to delete messages older than X days

Post by mattg » 2011-02-16 04:24

are your sent items in a 'Sent' folder? Not 'Sent Items' or 'Sent Messages'?

Do you use IMAP?
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

ozgurerdogan
Senior user
Senior user
Posts: 268
Joined: 2010-11-19 18:50

Re: Script to delete messages older than X days

Post by ozgurerdogan » 2011-04-13 15:31

Code: Select all

Const DAYS_TO_KEEP_MESSAGES = "45"   '' change as appropriate
Const MESSAGES_FOLDER = "Junk"     '' this will delete from the inbox folder, change as appropriate

Const HMSADMINUSER = "xxx"
Const HMSADMINPWD = "xxx"

Dim NumDeleted

'On Error Resume Next

Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  Dim oDomain
  Set oDomain = oApp.Domains.Item(x)  
   
  For y = 0 to oDomain.Accounts.Count - 1
    Dim oAccount
    Set oAccount = oDomain.Accounts.Item(y)
   
    Dim oMessages
    Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages

    Dim iMessages

    NumDeleted = 0
   
    WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & " messages"
   
    If oMessages.Count > 0 Then
          
      For iMessages = (oMessages.Count - 1) To 0 step -1

        Dim oMessage
        Set oMessage = oMessages.Item(iMessages)

        Dim vbDate
            
        vbDate = oMessage.InternalDate
         
        If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
          NumDeleted = NumDeleted + 1
          oMessages.DeleteByDBID(oMessage.ID)
        End If
      Next
    End If
   
    WScript.Echo "Removed " & NumDeleted & " message(s) from account " & oAccount.Address
  
  Next
Next 

When I use above script, after checking and deleting some account's mails but not all, I am getting:

Command.vbs(24, 5) Microsoft VBScript runtime error: Subscript out of range

if I make uncomment 'On Error Resume Next, it seems to be working fine. So do you have any idea? My server is Turkish religion
Should Ileave it as uncommented for 'On Error Resume Next line?

Thanks in advance.

ozgurerdogan
Senior user
Senior user
Posts: 268
Joined: 2010-11-19 18:50

Re: Script to delete messages older than X days

Post by ozgurerdogan » 2011-04-13 15:36

I found out why. If that acount does not have Junk folder. Then script creates that error. So you do you think I can uncomment;
'On Error Resume Next


Thanks :)

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-25 11:03

the subscript out of range is because if a folder doesn't exist in the email account then u get that error so i guess some code to check if folder exists is required

ozgurerdogan
Senior user
Senior user
Posts: 268
Joined: 2010-11-19 18:50

Re: Script to delete messages older than X days

Post by ozgurerdogan » 2011-04-25 11:43

yes you are right. So I had to uncomment On Error Resume Next part.

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-25 13:01

so to solve that i have modified the script such as to check if the folder exists then only to continue or to move onto next account, but one problem which i don't seem to be able to solve, currently the script works on root folders such as spam, trash, etc but i doesn't work if u need to make it search for a sub folders such as inbox.spam and to make that work i guess the part where the folder check is done, some modifications need to be done

bytheway i have added domain and account active checks

Code: Select all

' confign start
Const DAYS_TO_KEEP_MESSAGES = "7" 'days old to keep mails
Const MESSAGES_FOLDER = "spam" ' folder to delete from, case incensitive
Const HMSADMINUSER = "Administrator" ' admin userid
Const HMSADMINPWD = "password" ' adminpass
' config end

Dim NumDeleted
Dim FoundFolder
Dim IMAPFolderNum
Dim SearchFolders
Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  	Dim oDomain
  	Set oDomain = oApp.Domains.Item(x)
	If oDomain.Active Then
		For y = 0 to oDomain.Accounts.Count - 1
			Dim oAccount
			Set oAccount = oDomain.Accounts.Item(y)
			If oAccount.Active Then
				Dim FindFolders
				Set FindFolders = oAccount.IMAPFolders
				FoundFolder = False
				For IMAPFolderNum = 1 To FindFolders.Count
				  Set SearchFolders = FindFolders.Item(IMAPFolderNum - 1)
				  If UCase(SearchFolders.Name) = UCase(MESSAGES_FOLDER) Then
					FoundFolder = True
					Exit For
				  End If
				Next
				If FoundFolder Then
					Dim oMessages
					Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages
					Dim iMessages
					NumDeleted = 0
					WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & " messages"
					If oMessages.Count > 0 Then
					  For iMessages = (oMessages.Count - 1) To 0 step -1
						Dim oMessage
						Set oMessage = oMessages.Item(iMessages)
						Dim vbDate
						vbDate = oMessage.InternalDate
						If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
						  NumDeleted = NumDeleted + 1
						  oMessages.DeleteByDBID(oMessage.ID)
						End If
					  Next
					End If
					WScript.Echo "Removed " & NumDeleted & " message(s) from account " & oAccount.Address
				Else
					WScript.Echo "Folder Not Found in account " & oAccount.Address
				End If
			End If
		Next
	End If
Next

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

Re: Script to delete messages older than X days

Post by mattg » 2011-04-26 02:36

xbipin wrote:...but one problem which i don't seem to be able to solve, currently the script works on root folders such as spam, trash, etc but i doesn't work if u need to make it search for a sub folders such as inbox.spam and to make that work i guess the part where the folder check is done, some modifications need to be done
This may help >> http://www.hmailserver.com/documentatio ... older_list
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-26 10:02

mattg wrote:
xbipin wrote:...but one problem which i don't seem to be able to solve, currently the script works on root folders such as spam, trash, etc but i doesn't work if u need to make it search for a sub folders such as inbox.spam and to make that work i guess the part where the folder check is done, some modifications need to be done
This may help >> http://www.hmailserver.com/documentatio ... older_list
i tried the code from that link, now the search for sub folders is done perfectly fine but the below code fails when trying to goto that folder to list its messages etc

Set oMessages = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages



UPDATED SCRIPT WITH SUBFOLDER SEARCH SUPPORT

Code: Select all

' confign start
Const DAYS_TO_KEEP_MESSAGES = "70" 'days old to keep mails
Const MESSAGES_FOLDER = "inbox.sent" ' folder to delete from, case incensitive
Const HMSADMINUSER = "Administrator" ' admin userid
Const HMSADMINPWD = "password" ' adminpass
' config end

Dim NumDeleted
Dim FoundFolder
Dim IMAPFolderNum
Dim SearchFolders
Dim FolderList
Dim aFolder
Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  	Dim oDomain
  	Set oDomain = oApp.Domains.Item(x)
	If oDomain.Active Then
		For y = 0 to oDomain.Accounts.Count - 1
			Dim oAccount
			Set oAccount = oDomain.Accounts.Item(y)
			If oAccount.Active Then
				Dim FindFolders
				Set FindFolders = oAccount.IMAPFolders
				FoundFolder = False
				FolderList = ListFolders(FindFolders, 0, "")
				aFolder = Split(Left(FolderList, Len(FolderList) - 1), "|")
				For each z in aFolder
					If UCase(z) = UCase(MESSAGES_FOLDER) Then
						FoundFolder = True
						WScript.Echo z
						Exit For
					End If
				Next
				
				If FoundFolder Then
					Dim oMessages
					Set oMessages  = oAccount.IMAPFolders.ItemByName(MESSAGES_FOLDER).Messages
					Dim iMessages
					NumDeleted = 0
					WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & 

" messages"
					If oMessages.Count > 0 Then
					  For iMessages = (oMessages.Count - 1) To 0 step -1
						Dim oMessage
						Set oMessage = oMessages.Item(iMessages)
						Dim vbDate
						vbDate = oMessage.InternalDate
						If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
						  NumDeleted = NumDeleted + 1
						  oMessages.DeleteByDBID(oMessage.ID)
						End If
					  Next
					End If
					WScript.Echo "Removed " & NumDeleted & " message(s) from account " & 

oAccount.Address
				Else
					WScript.Echo "Folder Not Found in account " & oAccount.Address
				End If
			End If
		Next
	End If
Next

Function ListFolders(obFolders, iRecursion, rootFolder)
  iRecursion = iRecursion +1
  Dim sMessage
  Dim i
  for i = 0 to obFolders.Count -1
  	Dim obFolder
	Set obFolder = obFolders.Item(i)
	if iRecursion > 1 then
		sMessage = sMessage & rootFolder & "." & obFolder.Name & "|"
	else
		sMessage = sMessage & obFolder.Name & "|"
	end if
	sMessage = sMessage & ListFolders(obFolder.SubFolders, iRecursion,  obFolder.Name)	
  Next
  iRecursion = iRecursion -1
  ListFolders = sMessage
End Function

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-26 16:49

any help with that line of code will allow me to add more features and reporting to this script asap

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-27 12:34

xbipin wrote:any help with that line of code will allow me to add more features and reporting to this script asap

plz don't bother, i have figured it out, will soon release the final script

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-27 15:45

finally got it working without any issues

(plz ignore poor coding style coz first time im ever doing some vb script)

Code: Select all

' confign start
Const DAYS_TO_KEEP_MESSAGES = "7" 'days old to keep mails
Const MESSAGES_FOLDER = "inbox.trash" ' folder to delete from, case insensitive
Const HMSADMINUSER = "Administrator" ' admin userid
Const HMSADMINPWD = "password" ' adminpass
' config end

Dim NumDeleted
Dim FoundFolder
Dim IMAPFolderNum
Dim SearchFolders
Dim FolderList
Dim aFolder
Dim oApp
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  	Dim oDomain
  	Set oDomain = oApp.Domains.Item(x)
	If oDomain.Active Then
		For y = 0 to oDomain.Accounts.Count - 1
			Dim oAccount
			Set oAccount = oDomain.Accounts.Item(y)
			If oAccount.Active Then
				Dim FindFolders
				Set FindFolders = oAccount.IMAPFolders
				FoundFolder = False
				FolderList = ListFolders(FindFolders, 0, "")
				aFolder = Split(Left(FolderList, Len(FolderList) - 1), "|")
				For each z in aFolder
					If UCase(z) = UCase(MESSAGES_FOLDER) Then
						FoundFolder = True
						Exit For
					End If
				Next
				If FoundFolder Then
					Dim oMessages
					Set oMessages  = GetInsideFolders(oAccount.IMAPFolders, MESSAGES_FOLDER)
					Dim iMessages
					NumDeleted = 0
					WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & " messages"
					If oMessages.Count > 0 Then
					  For iMessages = (oMessages.Count - 1) To 0 step -1
						Dim oMessage
						Set oMessage = oMessages.Item(iMessages)
						Dim vbDate
						vbDate = oMessage.InternalDate
						If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
						  NumDeleted = NumDeleted + 1
						  oMessages.DeleteByDBID(oMessage.ID)
						End If
					  Next
					End If
					WScript.Echo "Removed " & NumDeleted & " message(s) from account " & oAccount.Address
				Else
					WScript.Echo "Folder Not Found in account " & oAccount.Address
				End If
			End If
		Next
	End If
Next

Function ListFolders(obFolders, iRecursion, rootFolder)
  iRecursion = iRecursion + 1
  Dim sMessage
  Dim i
  for i = 0 to obFolders.Count -1
  	Dim obFolder
	Set obFolder = obFolders.Item(i)
	if iRecursion > 1 then
		sMessage = sMessage & rootFolder & "." & obFolder.Name & "|"
	else
		sMessage = sMessage & obFolder.Name & "|"
	end if
	sMessage = sMessage & ListFolders(obFolder.SubFolders, iRecursion,  obFolder.Name)	
  Next
  iRecursion = iRecursion -1
  ListFolders = sMessage
End Function

Function GetInsideFolders(obFolders2, subFolders)
	Dim iRecursion2
	iRecursion2 = 1
	Dim SeprateFodlers
	SeprateFodlers = Split(subFolders, ".")
	For each w in SeprateFodlers
		if iRecursion2 = 1 then
			Set obFolders2 = obFolders2.ItemByName(w)
			iRecursion2 = iRecursion2 + 1
		Else
			Set obFolders2 = obFolders2.SubFolders.ItemByName(w)
		End If
	Next
	Set GetInsideFolders = obFolders2.Messages
End Function

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-04-30 11:25

here is the final version of the script with features like, multiple folder mail deletion, email reporting and some minor code tweaks for stability and speed

Code: Select all

' confign start
Const DAYS_TO_KEEP_MESSAGES = "7" ' Days old to keep mails
Const MESSAGES_FOLDER = "spam|trash|deleted messages|deleted items|junk e-mail|inbox.spam|inbox.trash|inbox.deleted messages|inbox.deleted items|inbox.junk e-mail" ' Folder to delete from, case insensitive, subfolder delimiter needs to be as below (.) based on ur delimiter setting in hmailserver, multiple fodlers can be specified seprated by | (pipe)
Const IMAP_DELIMITER = "." ' This needs to be same as what u used above for subfodlers based on delimiter setting in hmailserver
Const HMSADMINUSER = "Administrator" ' Admin username
Const HMSADMINPWD = "password" ' Admin password
Const FROM_EMAIL = "email@domain.com" ' Replace this with the email address you want the report to come from
Const REPORT_TO_EMAIL = "email@domain.com" ' Replace this with the email address you want the report to be sent to
' config end

Dim mFolders
Dim oApp
Dim AccountSize
Dim NumMsgs
Dim NumDeleted
Dim FoundFolder
Dim FolderList
Dim aFolder
Dim Message

mFolders = Split(MESSAGES_FOLDER, "|")
Set oApp = CreateObject("hMailServer.Application")
Call oApp.Authenticate(HMSADMINUSER, HMSADMINPWD)

For x = 0 to oApp.Domains.Count - 1
  	Dim oDomain
  	Set oDomain = oApp.Domains.Item(x)
	If oDomain.Active Then
		For y = 0 to oDomain.Accounts.Count - 1
			Dim oAccount
			Set oAccount = oDomain.Accounts.Item(y)
			OutputMsg = OutputMsg & "<tr><td colspan=""5"">&nbsp;</td></tr>" & vbCrLf
			If oAccount.Active Then
				For each SpamFolder in mFolders
					AccountSize = 0
					NumMsgs = 0
					NumDeleted = 0
					xFolder = ""
					Dim FindFolders
					Set FindFolders = oAccount.IMAPFolders
					FoundFolder = False
					FolderList = ListFolders(FindFolders, 0, "")
					aFolder = Split(Left(FolderList, Len(FolderList) - 1), "|")
					For each z in aFolder
						If UCase(z) = UCase(Trim(SpamFolder)) Then
							FoundFolder = True
							SpamFolder = z
							Exit For
						End If
					Next
					If FoundFolder Then
						Dim oMessages
						Set oMessages  = GetInsideFolders(oAccount.IMAPFolders, SpamFolder)
						NumMsgs = oMessages.Count
						'WScript.Echo "In account " & oAccount.Address & " with " & oMessages.Count & " message(s) in " & SpamFolder & " folder"
						If oMessages.Count > 0 Then
						  Dim iMessages
						  For iMessages = (oMessages.Count) To 1 step -1
							Dim oMessage
							Set oMessage = oMessages.Item(iMessages - 1)
							AccountSize = AccountSize + oMessage.Size
							Dim vbDate
							vbDate = oMessage.InternalDate
							If (vbDate < CDate(Now - DAYS_TO_KEEP_MESSAGES)) Then
							  NumDeleted = NumDeleted + 1
							  oMessages.DeleteByDBID(oMessage.ID)
							End If
						  Next
						End If
						'WScript.Echo "Removed " & NumDeleted & " message(s) from " & SpamFolder & " folder in account " & oAccount.Address
						OutputMsg = OutputMsg & "<tr><td>" & oAccount.Address & "</td>"
						OutputMsg = OutputMsg & "<td>" & SpamFolder & "</td>"
						OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumMsgs, 0, True, False, True) & "</td>"
						OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(AccountSize, 0, True, False, True) & "K</td>"
						OutputMsg = OutputMsg & "<td align=""right"">" & FormatNumber(NumDeleted, 0, True, False, True) & "</td></tr>" & vbCrLf
					'Else
						'WScript.Echo SpamFolder & " folder Not Found in account " & oAccount.Address
					End If
				Next
			End If
		Next
	End If
Next

OutputMsg = "<font face=""Arial""><table border=""1""><tr><td><b>Email Account</b></td><td><b>Folder</b></td><td><b>Mail Count</b></td><td><b>Mail Size</b></td><td><b>Deleted</b></td></tr>" & vbCrLf & OutputMsg
OutputMsg = OutputMsg & "</table>" & vbCrLf
Set Message = CreateObject("hMailServer.Message")

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

Function ListFolders(obFolders, iRecursion, rootFolder)
  iRecursion = iRecursion + 1
  Dim sMessage
  Dim i
  for i = 0 to obFolders.Count -1
  	Dim obFolder
	Set obFolder = obFolders.Item(i)
	if iRecursion > 1 then
		sMessage = sMessage & rootFolder & IMAP_DELIMITER & obFolder.Name & "|"
	else
		sMessage = sMessage & obFolder.Name & "|"
	end if
	sMessage = sMessage & ListFolders(obFolder.SubFolders, iRecursion,  obFolder.Name)	
  Next
  iRecursion = iRecursion -1
  ListFolders = sMessage
End Function

Function GetInsideFolders(obFolders2, subFolders)
	Dim iRecursion2
	iRecursion2 = 1
	Dim SeprateFodlers
	SeprateFodlers = Split(subFolders, IMAP_DELIMITER)
	For each w in SeprateFodlers
		if iRecursion2 = 1 then
			Set obFolders2 = obFolders2.ItemByName(w)
			iRecursion2 = iRecursion2 + 1
		Else
			Set obFolders2 = obFolders2.SubFolders.ItemByName(w)
		End If
	Next
	Set GetInsideFolders = obFolders2.Messages
End Function

cerri
Normal user
Normal user
Posts: 90
Joined: 2008-05-12 17:06

Re: Script to delete messages older than X days

Post by cerri » 2011-06-28 19:44

The best will be to have a script to select folder to search and move messages instead of delete. :-)

cerri
Normal user
Normal user
Posts: 90
Joined: 2008-05-12 17:06

Re: Script to delete messages older than X days

Post by cerri » 2011-07-06 09:55

No one? :-(

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: Script to delete messages older than X days

Post by xbipin » 2011-07-06 09:58

i can do it, need some time though

cerri
Normal user
Normal user
Posts: 90
Joined: 2008-05-12 17:06

Re: Script to delete messages older than X days

Post by cerri » 2011-07-06 14:18

I'll be glad to wait :-)
I think that can be useful for many people, anyway :D

micros72
New user
New user
Posts: 2
Joined: 2011-07-19 13:21

Re: Script to delete messages older than X days

Post by micros72 » 2011-07-19 14:35

How do I implement this script? Can't find any good documentation about scripts.

Post Reply