Delete messages after 30 days from INBOX.spam

This section contains scripts that hMailServer has contributed with. hMailServer 4 is needed to use these.
Post Reply
aty154
New user
New user
Posts: 9
Joined: 2006-05-25 14:45
Location: Pakistan/Karachi
Contact:

Delete messages after 30 days from INBOX.spam

Post by aty154 » 2009-11-13 11:09

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 = "xxxxxx" ' Replace this with your administrator username
AdminPass = "xxxxxxx"  ' Replace this with your administrator password
FromEmail = "xxxx@xxxx.com" ' Replace this with the email address you want the report to come "from"
ReportToEmail = "xxxx@xxxx.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 = True ' 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 LCase(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


----------------------------------------

I run above script on my hmailserver version 4.4-B270 with mysql 4 on windows server 2003. Script didn't show any error or messages but not able to detect emails in spam folder and delete them.
I have difference in my hmailserver setup like spam emails are moved into IMAP folder "INBOX.spam" word "spam" is in small cap and secondly spam folder contains 6-7 months older emails which need to be deleted.

Please help me run this script.

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

Re: Delete messages after 30 days from INBOX.spam

Post by ^DooM^ » 2009-11-13 11:10

That script is written around the default spam mailbox that is setup on the root of the server not placed in the inbox. I suggest you move spam to this new folder or pay someone to change it for you to support sub folders.

Also if you want to empty inbox.spam login with an email client or webmail like squirrelmail and delete it.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Post Reply