How do you insert email headers with a Thunderbird extension?

I'm building a Thunderbird extension and would like to add my own header to all outgoing email (eg <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!


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 rn
     subject.gMsgCompose.compFields.otherRandomHeaders += "x-test: testrn"; 
     }

  },
  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.


I don't know the answer but just some thoughts...

I think thunderbird extensions are usually just xul and js. From the enigmail site:

Unlike most Mozilla AddOns, Enigmail contains platform dependent parts: it depends on the CPU, the compiler, libraries of the operating system and the email application it shall integrate into.

Looking at the Enigmail source code, this might be the relevant section (written in c++)

So you might need to either translate what they've done into js(!) or keep looking for a different example.

Here's another link that might be helpful

链接地址: http://www.djcxy.com/p/41946.html

上一篇: 面向组合的编程(COP),.NET 4.0,MEF和奥斯陆知识库

下一篇: 如何用Thunderbird扩展插入电子邮件标题?