6

Is it possible to write your own filters for Thunderbird, I don't care what language is used.

I want to be able to do a full analysis of the email (headers and text) and take certain actions based on the content.

Bonus points if I'm able to compare the current email with older received messages (mainly to remove duplicate log messages)

4 Answers 4

4

Actually it is doable with FiltaQuilla. (Tried FiltaQuilla 1.1.0, with Thunderbird 3.1.9).

Below is a test script put under "javascript action with body" supplied by FiltaQuilla. It does show all the header properties and the body in the error console.

Once those data fields are available to the code, it needs to traverse through messenger folders and messages to compare them. Maybe using the "threadParent" property on the hdr can simplify the logic, refer to nsIMsgDBHdr on MDC for a hint. In the code, extra properties can be set. For example, set "hdr.myvar='1234'" in the code for duplicated messages, then in a later filter match myvar to 1234 and do a normal action to delete them.

Since this script is the action part of a filter, the condition part of the filter needs to match all messages of interest. In this test the script is arranged after an action to copy the message to a local folder, that may or may not be necessary but it does download the message body to the local machine on an IMAP account so the action will do have a body to access.

//FiltaQuilla filter action script: 
for (let index = 0; index < msgHdrs.length; index++)
{ //get msg hdr:
  let hdr = msgHdrs.queryElementAt(index, Ci.nsIMsgDBHdr);

  //show subject: get it in two ways
  let s = hdr.getStringProperty("subject");
  if ( s != undefined && s.length != undefined && s.length > 0)
      Cu.reportError("  flt log1: " + s.length + " " + s);
  Cu.reportError("  flt log2: " + hdr.subject );

  //show all properties of hdr: uncomment to show.
  //let e = hdr.propertyEnumerator;
  //while ( e.hasMore() ) Cu.reportError("  flt log3: " + e.getNext() ); 

  //get body: from an example on MDC: getMessageBody(aMessageHeader)
  //Actions configured in the filter: Without [1], it takes long long time. 
  //    [1] copy msg to a local folder. 
  //    [2] "Javascript Action with Body" with this script. 
  { let messenger = Components.classes["@mozilla.org/messenger;1"]
                      .createInstance(Components.interfaces.nsIMessenger);
    let listener = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
                      .createInstance(Components.interfaces.nsISyncStreamListener);
    let uri = hdr.folder.getUriForMsg(hdr);
    messenger.messageServiceFromURI(uri)
                .streamMessage(uri, listener, null, null, false, "");
    let folder = hdr.folder;
    let body = folder.getMsgTextFromStream(listener.inputStream,
                              hdr.Charset, 65536, 32768, false, true, { });
    //show body:
    Cu.reportError("  flt log4: " + body.length + " " + body);
  }
}
3
  • 1
    as of 2023, line 4 needs to read let hdr = msgHdrs.queryElementAt ? msgHdrs.queryElementAt(index, Ci.nsIMsgDBHdr) : msgHdrs[index];
    – joh6nn
    May 10, 2023 at 12:59
  • 1
    Errors out for me in TB115. Console says "TypeError: messenger.messageServiceFromURI is not a function".
    – John Smith
    Oct 16, 2023 at 21:42
  • 1
    Also, in 115 "propertyEnumerator" is now called "properties", and doesn't have a hasMore() function anymore.
    – John Smith
    Oct 16, 2023 at 21:49
4

No, this doesn't seem to be possible.

Such processing is probably easiest to do on the mail server, with stuff like procmail, Courier maildrop, amavisd-new, etc.

Amavisd-new is specifically designed to pass mail to external processing programs before delivery, usually anti-virus and anti-spam daemons.

If you cannot do it on the server side, you would need to write an extension, but that is probably far too much work.

Finally, maybe you can satisfy a good part of your needs with the FiltaQuilla extension, which supports regular expressions, running external programs, and javascript actions. You can find more info on the author's page.

Personally, if I didn't have access to the mail server and if FiltaQuilla didn't satisfy my needs, I would chose to set up a local Postfix / IMAP mail server fetching the mail from the ISP and processing it before delivery with my custom scripts. It should be much easier than writing a TB extension. But it may be overkill for your needs.

1
  • Wonder why this has been "unaccepted", when it was actually the first to mention, among other possibilities, the apparently preferred FiltaQuilla extension.
    – mivk
    Oct 2, 2011 at 15:02
2

You could look for a POP3/IMAP proxy that has the scriptable filtering capabilities you need. It could be a proxy which runs on the same PC as Thunderbird.

1

With the extension: http://mesquilla.com/extensions/filtaquilla/

you can create JavaScript and/or external files to execute, so it looks like a solid "maybe"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .