0

I am using the

function MsgHdrToMimeMessage(aMsgHdr, aCallbackThis, aCallback,
                             aAllowDownload, aOptions) {

method from http://mxr.mozilla.org/comm-central/source/mailnews/db/gloda/modules/mimemsg.js#171 to read the selected email via thunderbird extension. This method works fine and the only trouble is that it gives the plain text message by stripping all the html from the message.

How to get a html version of the message instead?

Vik
  • 8,721
  • 27
  • 83
  • 168

1 Answers1

1

As I know you cannot access to the whole body (with mail and html tags). You have the functions and attributes of the XPCOM scriptable interface nsIMsgDbHdr. I have an add-on which sends mail. I read the whole mail body with the help of the following code snippet. As you can see I read the whole mail from the disk and loaded its content into a variable. You can also use it to read the full mail body.

function SendMailNow(aMsgDBHdr) {
    var aMsgURI = aMsgDBHdr.folder.getUriForMsg(aMsgDBHdr);

    var msgWindow = Components.classes["@mozilla.org/messenger/msgwindow;1"]
            .createInstance();
    msgWindow = msgWindow.QueryInterface(Components.interfaces.nsIMsgWindow);

    var msgStream = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
            .createInstance();
    msgStream = msgStream.QueryInterface(Components.interfaces.nsIInputStream);

    var aMsgService = messenger.messageServiceFromURI(aMsgURI);

    var scriptInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
            .createInstance();
    scriptInputStream = scriptInputStream
            .QueryInterface(Components.interfaces.nsIScriptableInputStream);

    scriptInputStream.init(msgStream);

    try {
        aMsgService.streamMessage(aMsgURI, // uri of message to stream
        msgStream, // a stream listener listening to the message
        msgWindow, // a nsIMsgWindow for progress and status feedback
        null, // a nsIUrlListener that is notified when url starts and stops
        false, // it will create a stream converter from message rfc2822 to
        null // Header added to the URI. e.g., header=filter
        );
    } catch (ex) {
    }

    // Creating content
    var content = "";
    while (scriptInputStream.available()) {
        content = content + scriptInputStream.read(512);
        if (content.match(/\r\n\r\n/) || content.match(/\n\n/)) {
            if (sendMail(content, aMsgDBHdr.messageId)) {
                log("SEND_DONE\t" + aMsgDBHdr.messageId + "\t"
                        + aMsgDBHdr.subject);
            } else {
                log("SEND_FAILED\t" + aMsgDBHdr.messageId + "\t"
                        + aMsgDBHdr.subject);
            }
        }
    }
}

I hope this will help you!

csikos.balint
  • 1,107
  • 2
  • 10
  • 25
  • this code works but printing lot of header data. is there a way to print starting from the message body? – Vik Sep 30 '13 at 23:00
  • I don't know any magic to do this. You have parse the content on your own by Javascript string manipulation [functions](http://w3schools.com/jsref/jsref_obj_string.asp) like `replace()`, `match()`, etc. As I can see [here](http://stackoverflow.com/questions/1672144/parsing-email-conversations-with-regular-expressions) you can find useful regexps for this. – csikos.balint Oct 01 '13 at 09:21
  • well that would be a workaround to strip out the text and get desired results. My intention was to know if there is some other method or way which gives only body – Vik Oct 01 '13 at 16:42
  • I wanted to write an extension that shows the first two or three words for each mail in a column. The message URI is obtained by `gDBView.getURIForViewIndex(row)`. This works, but Thunderbird crashes on first call to `scriptInputStream.available()` - even if I call that for only one single row. Do you have an idea what I'm doing wrong? – steffen Feb 03 '18 at 21:37