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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With