Wednesday, February 11, 2009

Thunderbird Extension Development

I've spent the last two weeks developing a thunderbird(tb) extension and I must say trying to find information specific to tb development is very hard to come by. Firefox plugin development information is all over the place. But tb resources are hard to come by. I have put together a quick reference that will hopefull get crawled by google and maybe give some help to others that are trying to do the same thing. Please not that this is just a qucik reference, of things I foudn to get the job done...they might not be the best method of doing things but it gets the job done.

Thunderbird Extension Development

Overlay Reference:

  • chrome://messenger/content/messenger.xul- statusbar

  • chrome://messenger/content/mailWindowOverlay.xul- Toolbar Menu (taskPopup), Message Context Menu (threadPaneContext)

  • chrome://messenger/content/msgHdrViewOverlay.xul- Attachment Context Menu (attachmentListContext)

Accessing Attachments

  1. var attachmentList = document.getElementById( 'attachmentList' ) // returns attachment list

  2. var selectedAttachments = attachmentList.selectedItems; //Get selected attachments

Getting Message Information

var MessageUri = GetFirstSelectedMessage(); // Get Message URI

/* Get message body as it appears in preview window */

body = document.getElementById("messagepane").contentDocument.body.innerHTML;

Accessing Limited Message Header information

  1. var msg = messenger.messageServiceFromURI(uri); // get message object

  2. var hdr = msg.messageURIToMsgHdr(uri); // get header object

  3. var subject = hdr.mime2DecodedSubject; // get Subject

  4. var from = hdr.mime2DecodedAuthor; // get From

  5. var to = hdr.mime2DecodedRecipients; // get To

  6. var msgdate = new Date(; // turn epoch into date

  7. var messageid = hdr.messageId; // get message ID

Downloading Attachments

This piece of code will take all selected attachments and download them to C:\ with their original filenames.

  1. attachments = this.getSelectedAttachments();

  2. for( i=0;i

  3. var attachment = attachments[i];

  4. /* Create a file interface object */

  5. var file = Components.classes[";1"].

  6. createInstance(Components.interfaces.nsILocalFile);

  7. var fullfilepath = 'C:/test/' + attachment.displayName;

  8. file.initWithPath(fullfilepath);

  9. if(!file.exists()) {

  10. file.create(0x00,0644);

  11. }

  12. messenger.saveAttachmentToFile( file, attachment.url, attachment.uri, attachment.contentType, null );

  13. }


Joseph Sant said...

Thank you kindly for this summary. Do you have a minor error on Line 10 where fullfile should really be fullfilePath. I wonder if you could venture an opinion on how difficult it would be to save an attachment, process that attachment using a local script then suck up the text output of the script into an edit or reply window for further annotation by the TB user.

Anonymous said...

Thank you very much for this!
I went to the same conclusion as you:
- FF tutorial stuff is pretty big, but TB stuff = 0 (almost)
- I was looking for these specific functions too without finding them

Thanx again and keep this blog working with future good remarks or ideas as you do

Donald J Organ IV said...

@JosephSant Not really you would have to have some type of delay in opening the reply window...but where there is a will there is a way. I know you can open local maybe once the attachment is downloaded do the processing by the plugin itself and not a local script and then you'll know when to open the reply window.

Donald J Organ IV said...

@Anonymous That is the exact reason I posted this. I havent done too much more TB plugin dev work but If you have questions you can always post them and I can see if I can find an answer.

Anonymous said...

thank you very much for this documentation. I would like to automatically save attachments of new messages. Because of that I think I need another source for the attachment array than this.getElementById(). Can you please help me.


Anonymous said...

Thank sir! I'm Wisuttipong from Thailand

Greg J Preece said...

Thanks for this information. It could prove very useful!

I've been trying to port my first Firefox extension to Thunderbird and couldn't figure out why it wouldn't appear, even though its linked JS was executing! After about an hour and a half of Googling, this pointed me in the right direction, and I eventually found out that the IDs for (roughly) the same containers in different apps are different. I've yet to see the MDC page that explains that, though it may well exist.

For the reference of others, Firefox's main toolbox (into which you place toolbars) is called "navigator-toolbox". In Thunderbird it's called "mail-toolbox". There's a description of how to use both here: