MozillaZine

Save Email as file

Talk about add-ons and extension development.
UseTheForceLuke
 
Posts: 28
Joined: August 3rd, 2010, 11:36 pm

Post Posted March 23rd, 2011, 7:48 am

Hey guys,

in my extension, I need to save a selected message as text/eml file.

Using the messenger.saveAs(...) method (like below) causes the filepicker to show up.
Is there any way to save the selected message as a file automatically (without any user confirmation)?

In my extension, I want to "duplicate" an email and replace several attributes such as the content and some header information.
I thought it would be easy to save the message as file, read and write stuff to it and then import it again...

The marked steps are problematic for me...

----
excerpt from my current code, where the file picker pops up
Code: Select all
mailmessageUri = aMsgHdr.folder.getUriForMsg(aMsgHdr);
...
messenger.saveAs(mailmessageUri,false,null,"C:\tmpMail.eml", true);

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

Post Posted March 23rd, 2011, 12:42 pm

The messenger.saveAs function uses the messenger.save.dir pref for the dir.

Code: Select all
var prefBranch = Components.classes["@mozilla.org/preferences-service;1"].
  getService(Components.interfaces.nsIPrefBranch);
prefBranch.setCharPref("messenger.save.dir", "C:");
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
messenger.saveAs(msgUri, true, null, "message.eml");
messenger.saveAs(msgUri, true, null, "message.htm");
messenger.saveAs(msgUri, true, null, "message.txt");

And the aBypassFilePicker param is not available until Thunderbird 3.3.

http://mxr.mozilla.org/comm-1.9.2/sourc ... senger.idl
http://mxr.mozilla.org/comm-central/sou ... senger.idl

Try this:

Code: Select all
var msgHdr = gFolderDisplay.selectedMessage;
var msgUri = msgHdr.folder.getUriForMsg(msgHdr);
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);
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();
var localFile = Components.classes["@mozilla.org/file/local;1"].
  createInstance(Components.interfaces.nsILocalFile);
localFile.initWithPath("C:\\message.eml");
var fileOutputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
  createInstance(Components.interfaces.nsIFileOutputStream);
fileOutputStream.init(localFile, 0x02|0x08|0x20, 0666, 0);
fileOutputStream.write(data, data.length);
fileOutputStream.close();

UseTheForceLuke
 
Posts: 28
Joined: August 3rd, 2010, 11:36 pm

Post Posted March 24th, 2011, 9:58 am

Thank you so very much, this helped me a lot!

Do you by any chance know how to import the edited file into the tb again?

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

Post Posted March 24th, 2011, 11:46 am

The ImportExportTools extension can import eml files.

http://www.nic-nac-project.de/~kaosmos/ ... rt-en.html

Blueworld9
 
Posts: 1
Joined: January 17th, 2017, 5:08 am

Post Posted January 17th, 2017, 5:15 am

Old post but I felt I had to say thank you to morat for saving my sanity.

Boy can it be difficult to find how to do what seems to be simple stuff "Save the currently selected email message to an eml file" in Thunderbird via code compared to VBA/Outlook. Still loving Thunderbird though and don't regret the move.

The above worked a treat for me just added the code into Custom Buttons Extension sorted.

DanRaisch
Moderator

User avatar
 
Posts: 115503
Joined: September 23rd, 2004, 8:57 pm
Location: Somewhere on the right coast

Post Posted January 17th, 2017, 6:33 am

Thanks for the input.

Locking due to the age (6 years) of the original posts.

Return to Extension Development


Who is online

Users browsing this forum: Bing [Bot] and 1 guest