Lotus Notes FAQ Visit Our Sponsor!

How do you automatically filter mail in Notes?

1. Create a folder in your mail database to hold the mail from the mailing list.
2. Create an agent in your mail database.
3. Give it a name.
4. Specify it as "If new mail has arrived".
5. Mark that the agent will run a script, as opposed to simple action or formula.
6. In the Event field, choose Initialize and enter the following script:

Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim collection As NotesDocumentCollection
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
For i=1 To collection.Count
  Set doc=collection.GetNthDocument(i)
  If doc.ReplyTo(0) = "lnotes-l@ozzie.notesnic.net" Then
    doc.PutInFolder( "Mailing List\L-Notes" )
    doc.RemoveFromFolder( "($Inbox)" )
  End If
Next
End Sub

This will place all mail from the mailing L-Notes mailing list and place it in the folder named "Mailing List\L-Notes" whenever the mail comes in. You can add additional Else statements for any other mailing lists you are subscribed to.
Your administrator will have to give you access to Run Restricted Agents in the server document for your mail server.

Another agent is from Richard Schwartz (rhs@rhs.com):

Dim folder List As String

folder("DOMINO-L@LISTSERV.OKSTATE.EDU") = "Lists\DOMINO-L"
folder("NOTES-L@LISTSERV.OKSTATE.EDU") = "Lists\NOTES-L"
folder("lnotes-l@ozzie.notesnic.net") = "Lists\LNOTES-L"
' more list entries go here...
'...
Set doc = coll.GetFirstDocument
While Not doc Is Nothing
moved = False
If doc.hasItem("SendTo")Then
For i = 0 To Ubound(doc.SendTo)
listIndex = doc.SendTo(i)
lt = Instr(listIndex,"<")
gt = Instr(listIndex,">")
If lt > 0 And gt > lt Then
listIndex = Mid(listindex,lt+1,gt-lt-1)
End If
If Iselement(folder(listIndex)) Then
doc.PutInFolder(folder(listIndex))
moved = True
End If
Next i
End If
If moved = False Then
If doc.hasItem("ReplyTo") Then
For i = 0 To Ubound(doc.ReplyTo)
listIndex = doc.ReplyTo(i)
lt = Instr(listIndex,"<")
gt = Instr(listIndex,">")
If lt > 0 And gt > lt Then
listIndex = Mid(listindex,lt+1,gt-lt-1)
End If
If Iselement(folder(listIndex)) Then
doc.PutInFolder(folder(listIndex))
moved = True
End If
Next i
End If
End If
If moved = True Then
doc.RemoveFromFolder("($Inbox)")
End If
Set doc = coll.GetNextDocument(doc)
Wend


My code deals with both the SendTo and ReplyTo fields -- I found that that was necessary in order to deal with all the various lists I get. The thing I really like about my code is that it uses the List data type, to store the destination folder names as an associative array indexed by the address string. This makes it very easy to maintain the code. I actually intended to write code that loads the List from documents in a configuration database, but it's so easy to go in and add another address to the List in code that I just never got around to it. The disadvantage of this method is that I couldn't use the Like operator, which means there has to be an exact match in the comparison. I wrote the code so that it would parse out the string between the "<"and "" signs, so at least I only have to get an exact match on that part. (I should have put in calls to lcase the addresses so that I wouldn't have to worry about matching case, but I never got around to doing that either).


Applies to Notes Versions: 4 4.5 4.6 5
Last Modified: August 2, 2001