Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you insert email headers with a Thunderbird extension?

Tags:

thunderbird

I'm building a Thunderbird extension and would like to add my own header to all outgoing email (e.g. <myext-version: 1.0> ). Any idea how to do this? I know it's possible since this is done in the OpenPGP Enigmail extension. Thanks!

like image 902
sgoel Avatar asked Oct 02 '08 12:10

sgoel


1 Answers

Here is the code from one extension I'm working on:

function SendObserver() {
  this.register();
}

SendObserver.prototype = {
  observe: function(subject, topic, data) {

     /* thunderbird sends a notification even when it's only saving the message as a draft.
      * We examine the caller chain to check for valid send notifications 
      */
     var f = this.observe;
     while (f) {
       if(/Save/.test(f.name)) {
           print("Ignoring send notification because we're probably autosaving or saving as a draft/template");
           return;
       }
       f = f.caller;
     }

     // add your headers here, separated by \r\n
     subject.gMsgCompose.compFields.otherRandomHeaders += "x-test: test\r\n"; 
     }

  },
  register: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                          .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(this, "mail:composeOnSend", false);
  },
  unregister: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                            .getService(Components.interfaces.nsIObserverService);
    observerService.removeObserver(this, "mail:composeOnSend");
  }
};


/*
 * Register observer for send events. Check for event target to ensure that the 
 * compose window is loaded/unloaded (and not the content of the editor).
 * 
 * Unregister to prevent memory leaks (as per MDC documentation).
 */
var sendObserver;
window.addEventListener('load', function (e) {if (e.target == document) sendObserver = new SendObserver(); }, true);
window.addEventListener('unload', function (e) { if (e.target == document) sendObserver.unregister();}, true);

Put this inside a .js file that is loaded by the compose window (for example by overlaying chrome://messenger/content/messengercompose/messengercompose.xul).

The check in SendObserver.observe was necessary in my case because I wanted to do a user interaction, but you could probably leave it out.

like image 124
gerhard Avatar answered Sep 24 '22 01:09

gerhard