MozillaZine

Putting active email subject and its content into variables

Talk about add-ons and extension development.
BeginnerDeveloper
 
Posts: 8
Joined: July 13th, 2017, 12:48 am

Post Posted July 21st, 2017, 1:01 am

I need a simple way to get an active email's subject and its content into js variables.
Can anyone post some example code?
I would be very grateful for your help.

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

Post Posted July 21st, 2017, 8:20 am

Try these:

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert("Subject: " + msgHdr.mime2DecodedSubject +
       "\nFrom: " + msgHdr.mime2DecodedAuthor +
         "\nTo: " + msgHdr.mime2DecodedRecipients +
       "\nDate: " + new Date(msgHdr.dateInSeconds * 1000).toLocaleString());

Code: Select all
// show message properties
var msgHdr = gFolderDisplay.selectedMessage;
var propEnum = msgHdr.propertyEnumerator;
var out = [];
while (propEnum.hasMore()) out.push(propEnum.getNext());
alert(out.join("\n"));

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert(msgHdr.getStringProperty("subject")); // undecoded "Subject:" header

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
var t1 = msgHdr.getUint32Property("date");
var t2 = msgHdr.getUint32Property("dateReceived");
t1 = new Date(t1 * 1000).toLocaleString(); // "Date:"
t2 = new Date(t2 * 1000).toLocaleString(); // "Received:"
alert(t1 + "\n" + t2);

nsIMsgDBHdr
http://developer.mozilla.org/en-US/docs ... sIMsgDBHdr

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

Post Posted July 22nd, 2017, 4:49 am

BeginnerDeveloper wrote:and its content

Do you want to stream the message?

Try these:

Code: Select all
// show message
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
var messenger = Components.classes["@mozilla.org/messenger;1"].
  createInstance(Components.interfaces.nsIMessenger);
var msgService = messenger.messageServiceFromURI(msgUri);
var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].
  createInstance(Components.interfaces.nsIScriptableInputStream);
var syncStreamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"].
  createInstance(Components.interfaces.nsISyncStreamListener);
scriptableInputStream.init(syncStreamListener);
var messageUri = msgService.streamMessage(msgUri, syncStreamListener, null, null, false, "", true);
var data = new String();
var count = scriptableInputStream.available();
while (count) {
  data = data + scriptableInputStream.read(count);
  count = scriptableInputStream.available();
}
scriptableInputStream.close();
alert(data);

Code: Select all
// show text
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
var messenger = Components.classes["@mozilla.org/messenger;1"].
  createInstance(Components.interfaces.nsIMessenger);
var msgService = messenger.messageServiceFromURI(msgUri);
var syncStreamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"].
  createInstance(Components.interfaces.nsISyncStreamListener);
msgService.streamMessage(msgUri, syncStreamListener, null, null, false, "", true);
var contentType = new Object();
var data = msgHdr.folder.getMsgTextFromStream(
  syncStreamListener.inputStream, msgHdr.Charset, 65536, 32768, false, true, contentType);
alert(contentType.value);
alert(data);

nsIMsgMessageService streamMessage
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgMessageService#streamMessage()

nsIMsgFolder getMsgTextFromStream
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgFolder#getMsgTextFromStream()

BeginnerDeveloper
 
Posts: 8
Joined: July 13th, 2017, 12:48 am

Post Posted July 23rd, 2017, 2:11 pm

morat wrote:Try these:

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert("Subject: " + msgHdr.mime2DecodedSubject +
       "\nFrom: " + msgHdr.mime2DecodedAuthor +
         "\nTo: " + msgHdr.mime2DecodedRecipients +
       "\nDate: " + new Date(msgHdr.dateInSeconds * 1000).toLocaleString());

Code: Select all
// show message properties
var msgHdr = gFolderDisplay.selectedMessage;
var propEnum = msgHdr.propertyEnumerator;
var out = [];
while (propEnum.hasMore()) out.push(propEnum.getNext());
alert(out.join("\n"));

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert(msgHdr.getStringProperty("subject")); // undecoded "Subject:" header

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
var t1 = msgHdr.getUint32Property("date");
var t2 = msgHdr.getUint32Property("dateReceived");
t1 = new Date(t1 * 1000).toLocaleString(); // "Date:"
t2 = new Date(t2 * 1000).toLocaleString(); // "Received:"
alert(t1 + "\n" + t2);

nsIMsgDBHdr
http://developer.mozilla.org/en-US/docs ... sIMsgDBHdr


Thank you very much! Your reply solved my problem instantly!

BeginnerDeveloper
 
Posts: 8
Joined: July 13th, 2017, 12:48 am

Post Posted July 31st, 2017, 7:13 am

morat wrote:
BeginnerDeveloper wrote:and its content

Do you want to stream the message?

Try these:

Code: Select all
// show message
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
var messenger = Components.classes["@mozilla.org/messenger;1"].
  createInstance(Components.interfaces.nsIMessenger);
var msgService = messenger.messageServiceFromURI(msgUri);
var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].
  createInstance(Components.interfaces.nsIScriptableInputStream);
var syncStreamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"].
  createInstance(Components.interfaces.nsISyncStreamListener);
scriptableInputStream.init(syncStreamListener);
var messageUri = msgService.streamMessage(msgUri, syncStreamListener, null, null, false, "", true);
var data = new String();
var count = scriptableInputStream.available();
while (count) {
  data = data + scriptableInputStream.read(count);
  count = scriptableInputStream.available();
}
scriptableInputStream.close();
alert(data);

Code: Select all
// show text
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
var messenger = Components.classes["@mozilla.org/messenger;1"].
  createInstance(Components.interfaces.nsIMessenger);
var msgService = messenger.messageServiceFromURI(msgUri);
var syncStreamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"].
  createInstance(Components.interfaces.nsISyncStreamListener);
msgService.streamMessage(msgUri, syncStreamListener, null, null, false, "", true);
var contentType = new Object();
var data = msgHdr.folder.getMsgTextFromStream(
  syncStreamListener.inputStream, msgHdr.Charset, 65536, 32768, false, true, contentType);
alert(contentType.value);
alert(data);

nsIMsgMessageService streamMessage
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgMessageService#streamMessage()

nsIMsgFolder getMsgTextFromStream
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgFolder#getMsgTextFromStream()


Can you help me with extracting email adress from an active email?
I need to extract an email adress of person who sends mail and person who is going to receive it from active mail.
Thank you in advance.

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

Post Posted July 31st, 2017, 11:46 am

Do you want to extract the mailbox without the name?

i.e.

Email Address = John Doe <johndoe@example.com>
Email Name = John Doe
Email Mailbox = johndoe@example.com

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert(msgHdr.mime2DecodedAuthor + "\n" + MailServices.headerParser.
  extractHeaderAddressMailboxes(msgHdr.mime2DecodedAuthor));

nsIMsgHeaderParser extractHeaderAddressMailboxes
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgHeaderParser#extractHeaderAddressMailboxes()

BeginnerDeveloper
 
Posts: 8
Joined: July 13th, 2017, 12:48 am

Post Posted July 31st, 2017, 11:57 pm

morat wrote:Do you want to extract the mailbox without the name?

i.e.

Email Address = John Doe <johndoe@example.com>
Email Name = John Doe
Email Mailbox = johndoe@example.com

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
alert(msgHdr.mime2DecodedAuthor + "\n" + MailServices.headerParser.
  extractHeaderAddressMailboxes(msgHdr.mime2DecodedAuthor));

nsIMsgHeaderParser extractHeaderAddressMailboxes
http://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIMsgHeaderParser#extractHeaderAddressMailboxes()


One more time, thank you very much Sir!

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 1 guest