Thunderbird Filters Javascript Accessing Message Body Content

FiltaQuilla is a great extension that enables the filter javascript to access the message headers and body content.

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
   let listener = Components
   let uri = hdr.folder.getUriForMsg(hdr);
        .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);

This is tried with FiltaQuilla 1.1.0, on Thunderbird 3.1.9.

There are other two ways of doing this on google code. The code of one of them is copied below. Never tried it though the code looks good.

var messageService = messenger.messageServiceFromURI(uri);
var messageStream = Components
var inputStream = Components
messageService.streamMessage(uri, messageStream, {},
                             null, false, null);

var body = "";
while (inputStream.available()) {
   body = body +;


There is a post on MDC regarding a demo extension that lists attachments, reads them, and accesses various properties.

The original writing was posted to stackexchange superuser as an answer to a question. Stackexchange is a great adventure by Mr Joel Spolsky and Mr Jeff Atwood.

About minghuasweblog

a long time coder
This entry was posted in All, Mozilla Firefox and Thunderbird and tagged , , , , , , . Bookmark the permalink.

3 Responses to Thunderbird Filters Javascript Accessing Message Body Content

  1. scriptam says:

    I don’t know if you are still using FileQuilla but i need your help. I want to create a filter that will parse the url in the body of my email (local folder) and open them in the navigator but i don’t know how… I don’t know the javascript…
    i copied this on internet but it don’t work :
    var matches = variableChaine.match(http:\/\/www\.monsite\.fr\/M[^’]*\.htm);

    for( var i in matches ){ matches[i] );
    Here a screenshot of my filter conf :

    Can you help me?

    • scriptam says:

      Best if it’s not the preview ^^

    • Hi, Thanks for reading my post! The approach you may try is to find an existing extension that does something similar to what you intend to do, and study its code. Before that, probably first read about how to develop for Mozilla extensions in general so as to get some background knowledge and be able to understand what the sample extension does. Wish this could be of some hints to you and wish your adventure a great success! -Minghua

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s