
OpenNTF Code Bin
About This Code
Brief Description:
Report as Spam (forward MIME, like View | Source)
Notes Version:
R6.x, R7.x
Last Modified:
01 Sep 2006
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
I've created a LotusScript agent and view action button that reports spam to an email address similarly to how you would do it manually (View | Source... and Forward... ) You may use this however you want (GPL) but please share your changes. Some improvements can be made (see todo in comment header), but it works well enough with 3rd party spam filters I've used.
======= BEGIN CODE ======= (select text below this line)
Sub Initialize
'Notes Agent: name=ReportAsSpam, trigger=onEvent, Action menu selection, target=All selected docs
'forwards the selected message(s) with full header to a spam reporting email address
'moves message from inbox to junk mail and appends "[Spam]" to the subject line
'requires: Function walkmime (mime As notesmimeentity) As String
'to-do: prevent reporting of whitelisted messages, prevent report of local domain addresses
'created 12/10/2005 by Ryan Benech
Const SPAMREPORTADDRESS = "spam@domain.com" 'the email address used to report spam, uncomment to go live
'Const SPAMREPORTADDRESS = "YourFullyQualified/NotesNameGoesHere" 'uncomment this line to debug messages
Dim sess As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim memo As NotesDocument, doc As NotesDocument
Dim col As NotesDocumentCollection
Dim mime As NotesMIMEEntity
Dim rtitem As NotesItem
Dim temp As Variant 'to get info from non-ASCII data
Dim i As Integer 'to count messages
Dim uiview As NotesUIView 'to update the inbox after messages have been moved
Dim mail As NotesRichTextItem 'the sent message
Dim item As NotesItem 'to process fields with multiple items (i.e. recieved)
sess.ConvertMime = False 'init mime conversion
Set db = sess.CurrentDatabase
Set col = db.UnprocessedDocuments
Set doc = col.GetFirstDocument()
If doc Is Nothing Then Exit Sub 'sanity check to abort early
Print "Reporting " + Cstr( col.Count ) + " selected documents as Spam to " + SPAMREPORTADDRESS + ". Please wait..."
i = 1
While Not(doc Is Nothing)
Set memo = db.CreateDocument
Set mail = New NotesRichTextItem( memo, "Body" )
Set mime = doc.GetMIMEEntity
If Not(mime Is Nothing) Then
Forall header In mime.HeaderObjects
Call mail.AppendText( walkmime ( mime ) ) 'calls required function walkmime
End Forall
Call doc.CloseMIMEEntities 'allow clean switch to using items
memo.subject = doc.subject(0)
Call memo.Send( False, SPAMREPORTADDRESS) 'forward spam report message
Print "Successfully reported message " Cstr(i) + " of " + Cstr( col.Count ) +" ("+ doc.Subject(0) + ")"
Else
Print "No MIME content found in message, not sending spam report. (the internet message may have been converted to Notes Format)"
End If
If Instr( doc.Subject(0), "[SPAM]" ) = 0 Then
Call doc.ReplaceItemValue("Subject", "[SPAM] " + doc.Subject(0) ) 'remove [spam]
End If
Call doc.Save(True,True)
Call doc.PutInFolder( "($JunkMail)" ) 'swap ($JunkMail) for ($Inbox) to perform a "not spam" action
Call doc.RemoveFromFolder( "($Inbox)" ) 'vice versa for this line... :-)
Call sess.UpdateProcessedDoc( doc )
Set doc = col.GetNextDocument(doc)
i = i +1 'increment counter
Wend
Set uiview = ws.CurrentView
Call uiview.DeselectAll 'to prevent UI bugs
sess.ConvertMime = True 'revert mime conversion
Beep
Print "Done reporting " + Cstr( col.Count ) + " document(s) as Spam."
End Sub
Function walkmime (mime As notesmimeentity) As String
'returns string of complete mime message
'created: 12/10/05 by Ryan Benech
'closely based on code contributed by Kim Dowds from Lotus Sanbox: "SpamAssassin for Notes"
Dim mime2 As notesmimeentity 'for walking mime tree
walkmime = "" 'init string
While Not (mime Is Nothing)
walkmime = walkmime & Chr(10) _
& mime.Headers & Chr(10) _
& mime.ContentAsText
Set mime2 = mime.GetFirstChildEntity 'get first child:
If mime2 Is Nothing Then 'none? then get next sibling
Set mime2 = mime.GetNextSibling
End If
If mime2 Is Nothing Then 'none? then get aunt/uncle :
Set mime2 = mime.GetParentEntity
If Not (mime2 Is Nothing) Then
Set mime2 = mime2.GetNextSibling
End If
End If
Set mime = mime2
Wend
End Function
======= END CODE ======= (select text above this line)
Usage / Example
To use, paste this code into your mail template as an agent named "Report As Spam" (see first line of comments for agent properties) and customize with your spam report email address. Then create a new Action that calls "Report as Spam" from your ($Inbox) folder. As a bonus, you can easily adapt this code in another agent called "Report As Good" to report a message flagged as spam to a "not spam" address by changing the recipeints.
Comments
Posted by Andy Donaldson on 09/01/2006 10:32:54 AMAwesome code, but a question
How would I be able to send to multiple addresses?
Posted by Ryan Benech on 09/01/2006 01:46:30 PMHow to send to multiple addresses
If you don't mind not seeing all the email addresses listed in the Print statements and sloppy code, you can add additional .send() functions just after this line:
"Call memo.Send( False, SPAMREPORTADDRESS) 'forward spam report message"
so, add something like this:
Call memo.Send( False, "someotheraddress@domain.com")
Call memo.Send( False, "bigbrother@domain.com")
Otherwise, the "correct" way to do it (sends only 1 message to the server per spam) would be to add an array of the recipients, like this:
Const SPAMREPORTADDRESS = "spam@domain.com, someotheraddress@domain.com, bigbrother@wherever.com"
Dim addresses(3) as String
addresses(1) = "first@domain.com"
addresses(2) = "second@domain.com"
addresses(3) = "third@domain.com"
Then change this line to use the array instead of the constant
"Call memo.Send( False, SPAMREPORTADDRESS) 'forward spam report message"
change to:
"Call memo.Send( False, addresses()) 'forward spam report message"
You could also change the constant so the print statements work well.
Posted by Andy Donaldson on 10/30/2006 10:46:04 AMOne more question please
What code can I use if I want to use doc.remove to delete the message? I tried using $Trash instead of $JunkMail, but when I do that, it places the memo into "limbo". Doesn't show up in any views other then all docs, and won't be deleted with the soft delete settings. Ideas?
Posted by Ryan Benech on 10/30/2006 08:48:24 PM$Trash instead of $JunkMail
You will need to replace the "putInFolder" function with a "remove" function to delete the document (and put it in the trash folder). The $Trash folder is a special design element that only shows deleted documents. Any "removed" document will honor your "soft delete" settings...
Comment or remove this line: "Call doc.PutInFolder( "($JunkMail)" )"
Add this line: "Call doc.remove( true )"
Posted by Andy Donaldson on 10/31/2006 07:32:38 AM"Function requires a valid ADT argument."
That's where I am stuck right now. I have the call doc.remove(true) line there, but I keep getting the error "Function requires a valid ADT argument." Suggestions?