Filter Incoming Mail

This is an archived page. It's not actively maintained.

To filter incoming mail, the first step is to detect new messages inconming. The, you've got Components.interfaces.nsIMsgDBHdr from where you can modify the mail. By example, to modify the mail subject :

var newMailListener = {
  msgAdded: function(aMsgHdr) {
    if( !aMsgHdr.isRead ) {
      // Here we are, a new mail has popped
      // let get a Javascript string object from the subject property
      // Querying mime conversion interface
      var mimeConvert = Components.classes[";1"]
      var subject =  mimeConvert.decodeMimeHeader(aMsgHdr.subject, null, false, true);
      // Here with have a string to modify with Javascript. By example, setting it to lower case
      subject = subject.toLocaleLowerCase();
      // Then we rebuild a subject objet by rencoding the string
      // and assign it to the message headers and we're done
      aMsgHdr.subject = mimeConvert.encodeMimePartIIStr_UTF8(subject, false, "UTF-8", 0, 72);
function init() {
  var notificationService = Components.classes[";1"]
  notificationService.addListener(newMailListener, notificationService.msgAdded);
addEventListener("load", init, true);

Have a look to nsIMsgDBHdr to get the full list of properties to be modified.

You can take a look at a working code in the CapsKiller add-on repository.