Getting mail sender in a Thunderbird extension

自古美人都是妖i 提交于 2019-12-10 10:54:19

问题


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 );

回答1:


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);
    }
  }
}



回答2:


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



来源:https://stackoverflow.com/questions/2063635/getting-mail-sender-in-a-thunderbird-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!