Automate the download of messages from an imap account

Discussion of general topics about Mozilla Thunderbird
Post Reply
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Automate the download of messages from an imap account

Post by Gloops »

Hello everybody,
This is what I do when there is no place any more on my imap server :
  • Create a local folder : . right click on "local folders" . create a new folder . put a name form the account name and the date
  • Select all the messages in the imap folder
  • Right click, save to : select the newly created folder
  • Wait for the messages to be copied
  • Read the first one and the last one, on the source, on the target, and roughly compare
I should like to automatize that (except perhaps the comparison). Any hints ?

Perhaps you need my profile. 10 years on .Net (C#), apart from that I use Javascript some time to time. Some other languages, not necessarily so relevant.
User avatar
tanstaafl
Moderator
Posts: 49647
Joined: July 30th, 2003, 5:06 pm

Re: Automatize the download of messages from an imap account

Post by tanstaafl »

Create a message filter to do that and manually run it via "tools -> run filters on folder" rather than configuring it to run automatically when checking for new mail. That doesn't require any programming knowledge.

After doing that use file -> empty trash and file -> compact folders to guarantee that all of the physical space was freed. There are plenty of free windows automation tools such as AutoIt or AutoHotkey that could be used to automate using the keyboard/mouse to run the three commands using a single keyboard shortcut if you need to do this frequently enough for it to be a concern. Those tools don't "know" about Thunderbird, they're generic ways to automate tasks.
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Re: Automatize the download of messages from an imap account

Post by Gloops »

OK, thank you.
That does not promise to integrate the current account name and the date in the name of the folder, does it ?
User avatar
tanstaafl
Moderator
Posts: 49647
Joined: July 30th, 2003, 5:06 pm

Re: Automatize the download of messages from an imap account

Post by tanstaafl »

It does nothing to the folder name. Perhaps you should consider configuring the IMAP account to archive using a folder in "Local Folders", selecting all of the messages and then pressing 'A' to archive them instead.

http://kb.mozillazine.org/Archiving_your_e-mail
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Re: Automatize the download of messages from an imap account

Post by Gloops »

Hello,
That can be a path of search, but the dialog box in the account parameters uses a list for that, so that will not be easy to set from an outside program.
I saw that "File / Save the selected messages", seems to be easier to drive that way. The program will have created the folder, and after selection of eml format can type the path in the dialog box.
There are two reservations about that :
  • I can only designate the format by its ordinal place in the list, and shall have to recompile in case that changes in future versions
  • this is very quick but uses a different format than what I used until there ; perhaps exporting the existing folders can be an option, but that will take time
User avatar
tanstaafl
Moderator
Posts: 49647
Joined: July 30th, 2003, 5:06 pm

Re: Automatize the download of messages from an imap account

Post by tanstaafl »

"That can be a path of search, but the dialog box in the account parameters uses a list for that, so that will not be easy to set from an outside program."

I'm confused. Is it you really want to write a C# program to solve your problem instead of using an existing free utility like AutoIt? If so, please explicitly say so. Or is the problem that you think you need to write a C# program in order to customize what the child folder names will be?

If the latter take a look at https://addons.thunderbird.net/en-US/th ... la/?src=ss and https://quickfilters.quickfolders.org/filtaquilla.html . That add-on adds many enhancements to the message filters, including the ability to execute whatever javascript code you want as an action. You can combine moves with complex actions. You'll need to do some experimentation to find out if you can use javascript to create folders (with the name you want).
morat
Posts: 6427
Joined: February 3rd, 2009, 6:29 pm

Re: Automatize the download of messages from an imap account

Post by morat »

Here is how to copy the selected messages to a newly created local folder using chrome code.

Code: Select all

(function () {
  function copyListener(aFolder) {
    this.OnStartCopy = function() {};
    this.OnProgress = function(aProgress, aProgressMax) {};
    this.SetMessageKey = function(aKey) {};
    this.GetMessageId = function(aMessageId) {};
    this.OnStopCopy = function(aStatus) {
      if (Components.isSuccessCode(aStatus)) {
        aFolder.updateFolder(msgWindow);
        gFolderTreeView.selectFolder(aFolder);
      }
    };
  }
  function folderListener() {
    this.OnItemAdded = function(aParentItem, aItem) {
      try {
        var destFolder = aItem.QueryInterface(Components.interfaces.nsIMsgFolder);
        var sourceFolder = GetFirstSelectedMsgFolder();
        var msgHdrs = gFolderDisplay.selectedMessages;
        var mutableArray = Components.classes["@mozilla.org/array;1"].
          createInstance(Components.interfaces.nsIMutableArray);
        for (var i = 0; i < msgHdrs.length; i++) {
          var msgHdr = msgHdrs[i];
          mutableArray.appendElement(msgHdr, false /*weak*/);
        }
        var cs = Components.classes["@mozilla.org/messenger/messagecopyservice;1"].
          getService(Components.interfaces.nsIMsgCopyService);
        cs.CopyMessages(sourceFolder, mutableArray, destFolder,
          false /*isMove*/, new copyListener(destFolder), msgWindow, false /*allowUndo*/);
      } catch(e) {}
      mailSession.RemoveFolderListener(listener);
    };
    this.OnItemRemoved = function(aParentItem, aItem) {};
    this.OnItemPropertyChanged = function(aItem, aProperty, aOldValue, aNewValue) {};
    this.OnItemIntPropertyChanged = function(aItem, aProperty, aOldValue, aNewValue) {};
    this.OnItemBoolPropertyChanged = function(aItem, aProperty, aOldValue, aNewValue) {};
    this.OnItemUnicharPropertyChanged = function(aItem, aProperty, aOldValue, aNewValue) {};
    this.OnItemPropertyFlagChanged = function(aItem, aProperty, aOldFlag, aNewFlag) {};
    this.OnItemEvent = function(aItem, aEvent) {};
  }
  var mailSession = Components.classes["@mozilla.org/messenger/services/session;1"].
    getService(Components.interfaces.nsIMsgMailSession);
  var listener = new folderListener();
  var notifyFlags = Components.interfaces.nsIFolderListener.all;
  mailSession.AddFolderListener(listener, notifyFlags);
  var am = Components.classes["@mozilla.org/messenger/account-manager;1"].
    getService(Components.interfaces.nsIMsgAccountManager);
  var folder = am.localFoldersServer.rootFolder;
  if (folder instanceof Components.interfaces.nsIMsgLocalMailFolder) {
    folder.createLocalSubfolder("Cowabunga");
  } else {
    folder.createSubfolder("Cowabunga", msgWindow);
  }
})();
Similar thread: http://forums.mozillazine.org/viewtopic ... &t=3007865

Reference
http://searchfox.org/comm-esr78/source/ ... ervice.idl
http://searchfox.org/comm-esr78/source/ ... stener.idl
http://searchfox.org/comm-esr78/source/ ... Folder.idl
http://searchfox.org/comm-esr78/source/ ... Folder.idl

How to select all messages in the selected folder...

Code: Select all

goDoCommand("cmd_selectAll");
How to get the folder name...

i.e. username and date without @example.com string

Code: Select all

(function () {
  function getDateTime() {
    var now = new Date();
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    if (month.toString().length == 1) month = "0" + month;
    if (day.toString().length == 1) day = "0" + day;
    if (hour.toString().length == 1) hour = "0" + hour;
    if (minute.toString().length == 1) minute = "0" + minute;
    if (second.toString().length == 1) second = "0" + second;
    var dateTime = year + month + day + hour + minute + second;
    return dateTime;
  }
  var folder = GetFirstSelectedMsgFolder();
  alert(folder.server.username.split("@")[0] + " " + getDateTime());
})();
Last edited by morat on January 18th, 2021, 8:22 am, edited 1 time in total.
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Re: Automate the download of messages from an imap account

Post by Gloops »

Hello,
I am going to have a look, thanks.
I am only at the very beginning of using what was said before, as I have a rush on MVC. The regular expression is ready to convert the path that I get in Edition / Properties of the folder, to a folder name for the backup. So there are still a few steps after that.
You mean I have got to paste the functions you said to the console of Thunderbird ?
morat
Posts: 6427
Joined: February 3rd, 2009, 6:29 pm

Re: Automate the download of messages from an imap account

Post by morat »

I don't know how you are going to automate the steps. Maybe you are going to create a command line handler or a keyboard shortcut or a toolbar button or a filtaquilla javascript action.

The code snippets should work using the error console in Thunderbird 68 and 78.
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Re: Automate the download of messages from an imap account

Post by Gloops »

Hello,
I am just at the beginning of using what was said until now, the regular expression is ready to translate the path (from Edition / Folder properties) to a folder name for the export. As you see some steps remain.
What I understood until now does not allow a good control of the application, as I shall rely on keystroke combinations emulations.
If I got what you said, your proposal allows to avoid this pitfall, on the other hand I have to read everything carefully to understand what does what.
So thank you very much, and ... please be patient.
You mean I am supposed to copy your functions to the Thunderbird console, do not you ?
Gloops
Posts: 106
Joined: April 19th, 2006, 6:12 am

Re: Automate the download of messages from an imap account

Post by Gloops »

You know what ? For curiosity's sake I am going to pursue the investigations of the two open leads, but I realized that the solution is much more simple than that.
I opened the parameters of an account, set the archive parameters to a local path, and then to get place on the server I just have to select the messages and depress the A key.
I still have to do the same for the other accounts, and to do a backup to an external disk, I imagine that can be considered out of topic.
Thank you very much for your help.
Post Reply