Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get sender and recipients in Thunderbird extension upon sending message

168 views
Skip to first unread message

piscopo....@gmail.com

unread,
Sep 23, 2014, 9:12:23 AM9/23/14
to
I'm playing around trying to create a Thunderbird extension, one of the bootstrapped/restartless type (I mean, javascript code is not run from overlays. Instead listeners fire for various events).

At some point I'd like to check the sender and recipients (To, Cc, Bcc) when user decides to send the message, so to perform some checks on them.

I already have a number of event listeners set up and working, including one for compose-send-message event that gets properly fired when user confirms sending the message.

There, I'm not able to find how to get the sender email address as well as all recipients email addresses. I tried both with:

let fields = components.classes["@mozilla.org/messengercompose/composefields;1"].
createInstance(components.interfaces.nsIMsgCompFields)
and with:

let params = components.classes["@mozilla.org/messengercompose/composeparams;1"].
createInstance(components.interfaces.nsIMsgComposeParams);

let fields = params.composeFields;
but anyway fields.hasRecipients returns false, and e.g. fields.to is null (or empty, can't exactly recall). It looks like they're not being set by TB.

Of course I searched around, also in TB threads related to overlays extensions, but with no luck. There's a SO thread here (http://stackoverflow.com/questions/2063635/getting-mail-sender-in-a-thunderbird-extension), which does not seem to completely answer the question as it's only about the sender.
Other references: SO again (http://forums.mozillazine.org/viewtopic.php?f=19&t=1850495), MozillaZine (http://forums.mozillazine.org/viewtopic.php?f=19&t=1850495), TB stdlib (https://github.com/protz/thunderbird-stdlib/blob/master/send.js#L228).

I posted same question on SO as well: http://stackoverflow.com/questions/25989864/get-sender-and-recipients-in-thunderbird-extension-upon-sending-message

Joshua Cranmer 🐧

unread,
Sep 23, 2014, 11:54:38 AM9/23/14
to
On 9/23/2014 8:12 AM, piscopo....@gmail.com wrote:
> There, I'm not able to find how to get the sender email address as well as all recipients email addresses. I tried both with:
>
> let fields = components.classes["@mozilla.org/messengercompose/composefields;1"].
> createInstance(components.interfaces.nsIMsgCompFields)

nsIMsgCompFields and nsIMsgComposeParams are basically glorified
dictionary wrappers of information. The code you've posted is the overly
verbose XPCOM way of saying |new nsIMsgCompFields()|--and so you're
getting a completely empty dictionary instead of the filled-in
dictionary that you want.

I don't know the compose front-end code in gory detail, but you probably
want gMsgCompose.compFields.

--
Joshua Cranmer
Thunderbird and DXR developer
Source code archæologist

piscopo....@gmail.com

unread,
Sep 24, 2014, 3:45:53 AM9/24/14
to
On Tuesday, September 23, 2014 5:54:38 PM UTC+2, Joshua Cranmer 🐧 wrote:
Oh, I see. That makes sense ... I found it a bit strange to retrieve fields or params in a way that was closer (so to say) to a module import, or some kind of singleton.

Thanks for the pointer: I'll search for that.

piscopo....@gmail.com

unread,
Sep 24, 2014, 10:29:13 AM9/24/14
to
Just made a quick test: whithin compose-send-message event handler one can access event.currentTarget.gMsgCompose, thus from there I just logged to console event.currentTarget.gMsgCompose.compFields.

What I see is that .to, .cc and .bcc fields are correctly set, which is great. But then .from and .replyTo are empty.

As a side note: .hasRecipients is true, .subject is set, .characterSet and .defaultCharacterSet are set, plus other boolean fields as well.

piscopo....@gmail.com

unread,
Sep 24, 2014, 10:36:26 AM9/24/14
to
@Brandon McGinty-Carroll:

I received an email notification on your reply, but I cannot see it here in the thread.
There you also mentioned 'event.originalTarget.gMsgCompose.compFields' in event handler, and as I just said it seems to partially work.

Now I just need to find out the sender ...

piscopo....@gmail.com

unread,
Sep 24, 2014, 11:18:36 AM9/24/14
to
adding more details:
* `event.currentTarget.gMsgCompose.compFields` is populated as said before
* with `.target` instead of `currentTarget` nothing shows up
* with `.originalTarget` instead of `currentTarget` nothing shows up

piscopo....@gmail.com

unread,
Sep 25, 2014, 9:02:11 PM9/25/14
to
Now, I'm looking on how to get the sender. I searched and read about the identity thing. I also got a piece of code that extracts identity straight from compose window DOM:

var identityKey = document.getElementById("msgIdentity").value;
return MailServices.accounts.getIdentity(identityKey);

I'm trying to "convert" this one to work not from an overlay, but from the event handler.
I do not have a global "document" there, nor "window". I tried with 'event.currentTarget', as that should be the compose window node, but with no luck.

So now I'm stuck there.

Joshua Cranmer 🐧

unread,
Sep 25, 2014, 9:50:24 PM9/25/14
to
On 9/25/2014 8:02 PM, piscopo....@gmail.com wrote:

> Now, I'm looking on how to get the sender. I searched and read about the identity thing. I also got a piece of code that extracts identity straight from compose window DOM:
>
> var identityKey = document.getElementById("msgIdentity").value;
> return MailServices.accounts.getIdentity(identityKey);
>
> I'm trying to "convert" this one to work not from an overlay, but from the event handler.
> I do not have a global "document" there, nor "window". I tried with 'event.currentTarget', as that should be the compose window node, but with no luck.
If you have access to an nsIMsgCompose object, msgCompose.identity
should be exactly what you want.

Giuseppe Piscopo

unread,
Sep 26, 2014, 3:22:01 PM9/26/14
to
>
> If you have access to an nsIMsgCompose object, msgCompose.identity
>
> should be exactly what you want.
>
>
>
> --
>
> Joshua Cranmer
>
> Thunderbird and DXR developer
>
> Source code archæologist

Well, that made it!

Inside 'onOpenWindow' listener there was some code storing a local reference to some compose nsIDOMWindow interface, which had a property 'gMsgCompose'.

Also, inside 'compose-send-message' listener I had access to the same thing through 'event.currentTarget.gMsgCompose'.

From that 'gMsgCompose', I could grab '.identity', adn from there e.g. '.email'.

Thanks a lot! I was almost going to drop everything.

Cheers

Giuseppe Piscopo

unread,
Sep 26, 2014, 3:44:17 PM9/26/14
to
>
>
>
> Well, that made it!
>
>
>
> Inside 'onOpenWindow' listener there was some code storing a local reference to some compose nsIDOMWindow interface, which had a property 'gMsgCompose'.
>
>
>
> Also, inside 'compose-send-message' listener I had access to the same thing through 'event.currentTarget.gMsgCompose'.
>
>
>
> From that 'gMsgCompose', I could grab '.identity', adn from there e.g. '.email'.
>
>
>
> Thanks a lot! I was almost going to drop everything.
>
>
>
> Cheers

If anyone cares, a more complete answer can be found at http://stackoverflow.com/questions/25989864/get-sender-and-recipients-in-thunderbird-extension-upon-sending-message
0 new messages