UPDATE (2 sept 2014 13:01): Just to clarify, I'm interested in handling an incoming reply from someone else, so coming in via SMTP. If it is a reply to an email that is already in some mail folder, it should be stored in that folder too.

I have a Postfix (SMTP) + Dovecot (IMAP) setup, with my /home/sybren/Maildir directory storing my mail. I have a Sieve filter to ensure my work email ends up in the "work" IMAP folder. My IMAP folder structure is quite extensive, with several sub-folders, say work/research/subject1, work/research/subject2, work/education/subject3, etc.

As an example, let's say I send an email to my colleagues with the subject "AAAAAA". After sending it, I move it into the folder work/research/subject1. Now I would like their replies "Re: AAAAAA" to be stored in work/research/subject1 too, by Dovecot.

Email is handed over from Postfix to Dovecot using this setting in main.cf (all on one line)

mailbox_command = /usr/lib/dovecot/deliver -c 
                        /etc/dovecot/dovecot.conf -m "${EXTENSION}"

My main.sieve file now contains simple rules like:

require ["fileinto"];

if header :contains ["To", "Cc", "Bcc", "From"] "@work.nl" {
    fileinto "work";

UPDATE 2 (2 sept 2014 15:16): This is the "flow" that I would like to see for an incoming email:

  1. Postfix receives the email via SMTP.
  2. Postfix performs spam/virus scanning.
  3. Postfix hands the mail over to Dovecot for delivery.
  4. Dovecot inspects some database of message ID to mail folder mappings.
  5. If the In-Reply-To message ID is found, deliver to the corresponding mail folder.
  6. If not, use the Sieve script to determine the appropriate location.

Of course, the last two steps could theoretically be part of the same Sieve script.

I hope someone can help me out!



  • @dr-sybren did you ever discover how to accomplish this? – M-Pixel Jul 23 at 17:42
  • Unfortunately, no. – dr. Sybren Jul 26 at 15:32

There is special functionality IMAP-threads intended exactly for that purpose. http://tools.ietf.org/html/rfc5256 Modern IMAP servers including dovecot already have it. When mailbox is in maildir format a special index is built that count on message-id. When you reply on message, original ID is stored in the headers In-Reply-To: and References:. So IMAP-server can easily retrieve all the messages in the thread and display them sequentially despite of actual location.

Therefore you just have to ensure that your MUA is configured to use threading.

Some MUAs like RoundCube can turn threading on/off for each maildir folder separately. Some other simply turn it on/off globally.

| improve this answer | |
  • 2
    That helps when displaying a folder's contents, but the OP asked about filing mail to different folders. – mc0e Sep 1 '14 at 11:08
  • yeah, I really want to automatically store an incoming message, in the same folder as the message referred to in the In-Reply-To or References header. – dr. Sybren Sep 2 '14 at 11:07
  • But why? Can't imagine any reasonable cause for that. – Kondybas Sep 2 '14 at 11:47
  • I work at a university. I may have discussions via email with people, all related to a certain course. This means all mail of that discussion should go into the "work/education/coursename" IMAP folder. I would like the mail server to automatically store replies to the same discussion in that "work/education/coursename" folder. Some of those discussion partners may also be affiliated with a different course, so there is no fixed sender <-> course mapping. – dr. Sybren Sep 2 '14 at 13:35
  • 1
    But if MUA can organize messages into threads via IMAP functionality without physical moving, there is no reasons to move them to the certain folder. – Kondybas Sep 2 '14 at 15:13

I don't think you can server-side.

Typically storing a copy of sent messages is done by your e-mail client program. Some clients have an option "save replies with original message" or similar instead of storing all replies in a single "Sent Items" folder.

In most cases the default behaviour of the client will be to use the IMAP connection to place the your copy of the reply directly in the "Sent Items" or another IMAP folder you select for that purpose. That means that those replies don't pass through the Dovecot Local Delivery Agent nor any of filtering rules that emails normally pass through when they arrive over SMTP.

Sometimes your email client will have an option to always send a "Bcc:" to send a copy of the message to your own mailbox, instead of storing that copy over IMAP. In that case your response will come through the Dovecot LDA and the message filters you have set up, although that might require changing your rules because for instance instead of matching the sender you then would need to filter on the recipient.

Most of the more advanced clients simply don't care in which folder your sent items are actually stored and will simply try to generate a view with the complete conversation thread.

| improve this answer | |

Your IMAP server (ie Dovecot) is not responsible for deciding where to store an email. Your mail client (which might be webmail, or desktop/mobile software) copies the email to a folder of its choice. Sieve is unlikely to be involved.

For mail from the MTA, the MTA typically passes the mail to sieve, which then puts it in the appropriate imap folder (probably via a postfix's delivery script though, not through the imap interface). When the mail comes in via IMAP, it's generally just stored, not passed off to Sieve.

Dovecot plugins can intercept IMAP events and react to them. In theory a dovecot plugin could track when you move messages to a different folder, and keep information about them that can be used during email delivery. I'm not aware of any such project.

I suspect the semantics of what should happen in such a system might get complex and ambiguous enough that it just caused lost email and user confusion. It would be nice to have a smart system suggest where I might want to file an email to such that it's easier to do, but I don't think I'd want it to just automatically happen.

The closest thing I can think that's actually implemented is that I have heard of a bayesian filter being trained to select a mailbox to file new mail in based on training examples from the user. Sounds cool, but again, I expect the confusion from even a small proportion of mis-placed email would not be worth it. I forget the name of the tool.

It is more plausible for MUA software to have a feature where your replies get filed in the same folder as the message you are replying to. E.g. this is an option in Thunderbird. The semantics are much clearer in this case.

| improve this answer | |
  • 1
    "Your IMAP server (ie Dovecot) is not responsible for deciding where to store an email." -- it is, when an email comes in via SMTP. This is what I want to know about. – dr. Sybren Sep 2 '14 at 10:59
  • To clarify: I know about the options available to me when I send a reply. I want to know how to automatically file someone elses reply, based on what they were replying to. – dr. Sybren Sep 2 '14 at 11:08
  • When an email comes in via smtp, a delivery agent will be told where to put the email. It's still not dovecot making that decision. – mc0e Sep 2 '14 at 11:33
  • The delivery agent is Postfix, which hands it over to Dovecot, as I describe in the question itself (see mailbox_command). Dovecot is also the one running my Sieve script, not Postfix. – dr. Sybren Sep 2 '14 at 13:08
  • No, postfix invokes the delivery agent that's provided with dovecot. You are correct though that the plugins to dovecot (specifically sieve) are invoked from dovecot delivery agent and are doing some decision making. I don't think you'll find a plugin to do what you want though, and more importantly I don't think it could be predictable enough to be useful. – mc0e Sep 3 '14 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.