MozillaZine

Javascript to retrieve the headers of current message

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

Post Posted September 23rd, 2018, 11:14 am

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: 2958
Joined: February 3rd, 2009, 6:29 pm

Post Posted September 23rd, 2018, 11:13 pm

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

Similar thread: viewtopic.php?f=19&t=3031921

user2018
 
Posts: 8
Joined: September 23rd, 2018, 11:07 am

Post Posted September 24th, 2018, 12:55 am

Thanks a lot! The URL provided (viewtopic.php?f=19&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: 2958
Joined: February 3rd, 2009, 6:29 pm

Post Posted September 24th, 2018, 6:04 am

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/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgHeaderParser#parseHeadersWithArray()

user2018
 
Posts: 8
Joined: September 23rd, 2018, 11:07 am

Post Posted September 30th, 2018, 9:55 am

Great, thanks a lot! This solved the problem.

Return to Thunderbird General


Who is online

Users browsing this forum: No registered users and 1 guest