Getting mail sender in a Thunderbird extension
I am working on a very simple Thunderbird extension, which is supposed to alert the name of the sender along with the names of the recipients whenever a mail is sent. The problem is that gMsgCompose.compFields.from field is empty in the below snippet (the .to field works as expected), which handles the开发者_运维百科 "compose-send-message" event. What am I missing here?
function send_event_handler( evt ) {
var msgcomposeWindow = document.getElementById( "msgcomposeWindow" );
var msg_type = msgcomposeWindow.getAttribute( "msgtype" );
// do not continue unless this is an actual send event
if( !(msg_type == nsIMsgCompDeliverMode.Now || msg_type == nsIMsgCompDeliverMode.Later) )
return;
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
promptService.alert(window, "From", gMsgCompose.compFields.from);
promptService.alert(window, "To", gMsgCompose.compFields.to);
}
window.addEventListener( "compose-send-message", send_event_handler, true );
You can get the sending identity through the msgIdentity widget:
var identityWidget = document.getElementById('msgIdentity');
var fromIdentityKey= self.identityWidget.value;
Then, lookup identity information using the account manager:
var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]
.getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
for (var i = 0; i < accounts.Count(); i++) {
var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
var accountIdentities = account.identities;
for(var identCount = 0; identCount < accountIdentities.Count(); identCount++) {
var identity = accountIdentities.QueryElementAt(identCount,
Components.interfaces.nsIMsgIdentity);
if(identity.key == fromIdentityKey) {
// Identity found
alert(identity.email);
}
}
}
Or in TB 7.0+ more conveniently & simply as:
alert(gCurrentIdentity.email);
No need to getService()
and iterate, it's already set by TB, see https://dxr.mozilla.org/comm-central/source/mail/components/compose/content/MsgComposeCommands.js#71
精彩评论