Postfix aliases and duplicate e-mails, how to fix?

24,686

Solution 1

There is a way how to do it, but not using Postfix itself.

If you are using a reasonably recent Dovecot with Pigeonhole implementation of Sieve, there is a special extension that can be used for on-delivery deduplication. In Dovecot older than 2.2.18, the name of this extension is "vnd.dovecot.duplicate" and it's disabled by default, in newer releases the extension is called "duplicate" and should be already enabled, but enabling it explicitly won't harm.

/etc/dovecot/conf.d/90-sieve.conf:

plugin {
  sieve = ~/.dovecot.sieve
  sieve_dir = ~/sieve

  sieve_before = /mnt/mail/users/global_sieve/deduplicate.sieve
  # sieve_before2 = /mnt/mail/users/global_sieve/antispam.sieve

  sieve_extensions = +vnd.dovecot.duplicate  # for dovecot < 2.2.18
  #sieve_extensions = +duplicate             # for dovecot >= 2.2.18
}

Then create the sieve script to handle the deduplication itself (you can adjust the filename as you see fit).

/mnt/mail/users/global_sieve/deduplicate.sieve:

require "vnd.dovecot.duplicate"; # for dovecot < 2.2.18
# require "duplicate";           # for dovecot >= 2.2.18

if duplicate {
    discard;
    stop;
}

Compile the script using sievec and make sure the script is readable by dovecot user. More in Dovecot docs.

If you are using Cyrus, there is a a duplicate message delivery suppression feature that can be enabled using suppress_duplicates = yes.

Solution 2

Postfix has no idea about duplicate emails due to the way it's structured. It is possible to do what you're suggesting by using procmail as your delivery agent.

Essentially, each message coming from a client should be delivered with a unique Message-Id. In the case that it's delivered to multiple people, the Message-Id should be the same, so we save any Message-Id headers we've seen and discard and future ones that match that list.

From http://novosial.org/procmail/

:0 Wh: msgid.lock
| formail -D 8192 ~/.procmail/msgid.cache

Solution 3

A solution that work for me is add -o receive_override_options=no_address_mappings on master.cf

Here the doc: http://www.postfix.org/postconf.5.html#receive_override_options

Share:
24,686

Related videos on Youtube

aqua
Author by

aqua

Updated on September 17, 2022

Comments

  • aqua
    aqua almost 2 years

    I have aliases set up in postfix, such as the following:

    [email protected]:    [email protected], [email protected] ...
    

    When an email is sent to [email protected], and any of the recipients in that alias is cc:ed which is quite common (ie: "Reply all"), the e-mail is delivered in duplicates. For instance, if an e-mail is sent to [email protected] and [email protected] is cc:ed, it'll get delivered twice. According to the Postfix FAQ, this is by design as Postfix sends e-mail in parallel without expanding the groups, which makes it faster than sendmail. Now that's all fine and dandy, but is it possible to configure Postfix to actually remove duplicate recipients before sending the e-mail?

    I've found a lot of posts from people all over the net that has the same problem, but I have yet to find an answer. If this is not possible to do in Postfix, is it possible to do it somewhere on the way? I've tried educating my users, but it's rather futile I'm afraid...

    I'm running postfix on Mac OS X Server 10.6, amavis is set as content_filter and dovecot is set as mailbox_command. I've tried setting up procmail as a content_filter for smtp delivery (as per the suggestion below), but I can't seem to get it right. For various reasons, I can't replace the standard OS X configuration, meaning postfix, amavis and dovecot stay put. I can however add to it if I wish.

  • aqua
    aqua over 14 years
    If I understand the problem correctly, it isn't possible to solve in Postfix because Postfix sends the e-mails in parallel, ie it sends to [email protected] at the same time as it expands [email protected] and then again sends to [email protected]. Wouldn't the Procmail solution then possibly introduce a race condition where another unrelated e-mail (with a different message id) is sent in between the two e-mails in question and thus overwriting the cache making it so that duplicate e-mails are delivered anyhow?
  • aqua
    aqua over 14 years
    I'm trying to get your suggestion to work, but I'm not really sure how to combine this with dovecot delivery, which is used by OS X 10.5+ by default.
  • Philip Reynolds
    Philip Reynolds over 14 years
    If you're using dovecot as a delivery agent, you can use procmail as a content_filter which will allow you to perform the same task on a global level
  • aqua
    aqua over 14 years
    I have to confess that I probably am in way over my head here. I've been staring myself blind on the configuration parameters page for postfix and googling till my fingers bleed but I can't seem to figure out how to configure this. Any resources you might know of or nudges in the right direction would really be appreciated!
  • aqua
    aqua over 14 years
    I think I sort of understand how it works now, but there's already a content_filter set to amavis. This seems to be some sort of anti virus checker. I've read that it's possible to chain content_filters, but it seems rather unintuitive and the documentation is less than stellar. It's really quite ridiculous how difficult it is to just weed out duplicate e-mails. Design desicions regardless, this seems to me like it should be in there out of the box or at least not require these monstruos hacks.
  • aqua
    aqua over 14 years
    Yes, I read that, perhaps I should have updated my question. By design or not, it's still an issue. I understand why it's behaving the way it does and I understand that trying to find a solution in Postfix is futile. However, I've tried coming up with some content filter solution using Procmail and I just can't seem to get it right. It's highly likely that I'm not understanding content filters or procmail correctly, or both. In either case I still would like to fix this. Unfortunately, the setup as it is cannot be modified, only added to. I can't use sendmail instead of postfix, unfortunately.
  • aqua
    aqua over 14 years
    I updated the question with some more information but honestly, the link to the FAQ was in there to start with. I do appreciate any helpful suggestions though!
  • jeffatrackaid
    jeffatrackaid over 14 years
    Did not realize that was a link to the same content. Working in IT security, you become rather paranoid about clicking random links. Can you confirm that the duplicate messages have the same message IDs?
  • jeffatrackaid
    jeffatrackaid over 14 years
    Been a long day so I need to review your original post but what about this: postfix.org/postconf.5.html#duplicate_filter_limit
  • aqua
    aqua over 14 years
    I can indeed confirm that the duplicate messages have the same message IDs. Setting the duplicate_filter_limit does nothing to help unfortunately.
  • mklein9
    mklein9 over 9 years
    On Ubuntu 8.04 this solution worked perfectly for me with some minor adjustments. Each email user has a home directory which has a .procmailrc that is a sym link to a central file under /etc. This file contains the procmail recipe in the answer adjusted as follows: | /usr/bin/formail -D 8192 $PMDIR/msgid.cache where $PMDIR was previously defined in the system procmail file as $HOME/procmail. Put the users' .procmailrc and the procmail directory in the /etc/skel directory to ensure new user accounts get this set up automatically.
  • lkraav
    lkraav over 7 years
    Thanks a ton for pointing this mechanism out. That "finding a needle in a haystack" feeling.
  • lkraav
    lkraav over 7 years
    Discoveries during implementation: duplicate extension works at user level. I was trying to stop duplicates over multiple users To/Cc-d in a central fileinto mailbox. This doesn't work. Had to also set a postfix virtual_alias_maps catch-all entry, such as @domain.com [email protected], then the sieve duplicate check will hit catchall user's duplicate list.
  • Net Runner
    Net Runner almost 7 years
    Yeah, that's very similar to "finding a needle in a haystack" feeling
  • busythomas
    busythomas almost 4 years
    Hi @Pioz, thanks for your answer, I visited the official doc and I have not yet fully understand, because after I set this option on my master.cf, postfix starts ignoring aliases set in Goto column in virtual alias table. Is this settings was supposed to turn off alias / forwarding feature of postfix?
  • busythomas
    busythomas almost 4 years
    Thank you! This answer works for me. I setup 2 aliases, each one of them, has the same members. And then, I sent from [email protected] to the 2 aliases at the same time and I only received 1 message at each of the alias member inbox. Before I always got 2 copies at each alias member inbox.