Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Filter Incoming Mail

Filter Incoming Mail

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["@mozilla.org/messenger/mimeconverter;1"]
        .getService(Components.interfaces.nsIMimeConverter);
      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["@mozilla.org/messenger/msgnotificationservice;1"]
    .getService(Components.interfaces.nsIMsgFolderNotificationService);
  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.

Document Tags and Contributors

 Contributors to this page: wbamberg, Nfroidure, jllogan
 Last updated by: wbamberg,