MozillaZine

thunder bird get me subject from name and from email

Talk about add-ons and extension development.
nickhere
 
Posts: 27
Joined: September 29th, 2009, 1:57 pm

Post Posted July 11th, 2018, 6:26 pm

so far i have this
Code: Select all
var acctMgr = Components.classes["@mozilla.org/messenger/account-manager;1"]
                        .getService(Components.interfaces.nsIMsgAccountManager);
var accounts = acctMgr.accounts;
for (var i = 0; i < accounts.length; i++) {
  var account = accounts.queryElementAt(i, Components.interfaces.nsIMsgAccount);
  var rootFolder = account.incomingServer.rootFolder; // nsIMsgFolder
  //Application.console.log(rootFolder.prettiestName);
  if (rootFolder.hasSubFolders) {
    var subFolders = rootFolder.subFolders; // nsIMsgFolder
    while(subFolders.hasMoreElements()) {
      var folder = subFolders.getNext().QueryInterface(Components.interfaces.nsIMsgFolder);
      Application.console.log(rootFolder.prettiestName+" "+folder.prettiestName);
    }
  }
}


What I want in the console log is to add the number of msg in the in the folder

then i need to extract the "subject", "the from name" and "the from email address" of every msg in the folder
and write to the log.

should be easy for your tbird expert not for a newbie dom javascipt learner like me

morat
 
Posts: 2905
Joined: February 3rd, 2009, 6:29 pm

Post Posted July 12th, 2018, 6:44 am

Here is how to extract data from all messages in all folders in all accounts.

Code: Select all
function openTab(aString) {
  var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].
    createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
  converter.charset = "UTF-8";
  aString = converter.ConvertFromUnicode(aString);
  aString = aString.replace(/&/g, "&amp;");
  aString = aString.replace(/>/g, "&gt;");
  aString = aString.replace(/</g, "&lt;");
  aString = aString.replace(/"/g, "&quot;");
  aString = aString.replace(/'/g, "&apos;");
  var data = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">';
  data += "<html><head><title>Example</title>";
  data += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  data += "</head><body><pre>" + aString + "</pre></body></html>";
  var win = Components.classes["@mozilla.org/appshell/window-mediator;1"].
    getService(Components.interfaces.nsIWindowMediator).
    getMostRecentWindow("mail:3pane");
  win.document.getElementById("tabmail").openTab("contentTab", {
    contentPage: "data:text/html;charset=utf-8;base64," + btoa(data),
    background: true,
  });
}
function checkSubFolders(aFolder) {
  if (!aFolder.hasSubFolders) {
    return;
  }
  var enumerator = aFolder.subFolders;
  while (enumerator.hasMoreElements()) {
    var folder = enumerator.getNext().
      QueryInterface(Components.interfaces.nsIMsgFolder);
    if (!(folder instanceof Components.interfaces.nsIMsgNewsFolder)) {
      folders.push(folder);
    }
    checkSubFolders(folder);
  }
}
var am = Components.classes["@mozilla.org/messenger/account-manager;1"].
  getService(Components.interfaces.nsIMsgAccountManager);
var allServers = am.allServers;
var numServers = allServers.length;
var folders = [];
var out = [];
for (var i = 0; i < numServers; i++) {
  var server = allServers.
    queryElementAt(i, Components.interfaces.nsIMsgIncomingServer);
  checkSubFolders(server.rootFolder);
}
for (var i = 0; i < folders.length; i++) {
  var enumerator = folders[i].messages;
  while (enumerator.hasMoreElements()) {
    var hdr = enumerator.getNext().
      QueryInterface(Components.interfaces.nsIMsgDBHdr);
    var hp = MailServices.headerParser;
    var date = new Date(hdr.dateInSeconds * 1000).toLocaleString();
    out.push("Account: " + hdr.folder.username +
            "\nFolder: " + hdr.folder.name +
           "\nSubject: " + hdr.mime2DecodedSubject +
              "\nFrom: " + hdr.mime2DecodedAuthor +
              "\nName: " + hp.extractHeaderAddressNames(hdr.mime2DecodedAuthor) +
           "\nMailbox: " + hp.extractHeaderAddressMailboxes(hdr.mime2DecodedAuthor) +
                "\nTo: " + hdr.mime2DecodedRecipients +
              "\nDate: " + date);
  }
}
openTab(out.join("\n\n"));

Similar threads
viewtopic.php?f=39&t=3037008
viewtopic.php?f=39&t=3039734

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 3 guests