Javascript to retrieve the headers of current message

Discussion of general topics about Mozilla Thunderbird
Post Reply
user2018
Posts: 196
Joined: September 23rd, 2018, 11:07 am

Javascript to retrieve the headers of current message

Post by user2018 »

I am trying to get the headers of a selected message using Javascript. I am doing the following:

var hdr = gFolderDisplay.selectedMessage;
var messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
var msgHdr = messenger.msgHdrFromURI(uriStr);

var theStr = "Subject: " + msgHdr.subject + "\n" +
"Date: " + msgHdr.date + "\n" + "From: " + msgHdr.author + "\n" + "To: " +
msgHdr.recipients + "\n" + "CC: " + msgHdr.ccList + "\n" + "BCC: " +
msgHdr.bccList + "\n" + "\n" + messageBody;
alert(theStr);

This seems to be working, but there are two problems. First, the text is shown encoded as MIME (e.g. "=?UTF-8?B?..."). Second, the date is shown as a number, which is not useful to me. I would like to retrieve the string values exactly as they are shown in the Thunderbird's GUI (that is, properly encoded, which in my case is UTF-8). I had a look at the web and tried several things but I couldn't solve this problem.

Any idea is appreciated! Thanks a lot.
morat
Posts: 6405
Joined: February 3rd, 2009, 6:29 pm

Re: Javascript to retrieve the headers of current message

Post by morat »

You can inspect the message header object in the error console.

Similar thread: http://forums.mozillazine.org/viewtopic ... &t=3031921
user2018
Posts: 196
Joined: September 23rd, 2018, 11:07 am

Re: Javascript to retrieve the headers of current message

Post by user2018 »

Thanks a lot! The URL provided (http://forums.mozillazine.org/viewtopic ... &t=3031921) solved a major part of my problem. I can use the variables msgHdr.mime2DecodedSubject, msgHdr.mime2DecodedAuthor and msgHdr.mime2DecodedRecipients in order to show those headers with the correct encoding and format. However, there is no equivalent "msgHdr.mime2DecodedCC" and "msgHdr.mime2DecodedBCC" for the CC and BCC fields, so those headers are still shown incorrectly. I want to retrieved those fields in JavaScript variable (just seeing them in the message header object in the error console is not enough for me). Any ideas?

Thank you for any suggestion.
morat
Posts: 6405
Joined: February 3rd, 2009, 6:29 pm

Re: Javascript to retrieve the headers of current message

Post by morat »

Try using the parseHeadersWithArray method.

Code: Select all

var hdrParser = Components.classes["@mozilla.org/messenger/headerparser;1"].
  getService(Components.interfaces.nsIMsgHeaderParser);
var msgHdr = gFolderDisplay.selectedMessage;
// var addresses = msgHdr.recipients;
var addresses = msgHdr.ccList;
var uniqueAddresses = hdrParser.removeDuplicateAddresses(addresses, "");
var emailAddresses = {};
var names = {};
var fullNames = {};
var numAddresses = hdrParser.parseHeadersWithArray(uniqueAddresses, emailAddresses, names, fullNames);
alert(addresses + "\n" + uniqueAddresses);
alert(emailAddresses.value + "\n" + names.value + "\n" + fullNames.value);
nsIMsgHeaderParser parseHeadersWithArray
http://developer.mozilla.org/en-US/docs ... ithArray()
user2018
Posts: 196
Joined: September 23rd, 2018, 11:07 am

Re: Javascript to retrieve the headers of current message

Post by user2018 »

Great, thanks a lot! This solved the problem.
Post Reply