LOCALMAILFILTER(7)

πŸ“¦
version 2.0.6 Β· 3 versions
, in ubuntu stonking

πŸ’Œ localmailfilter(7) β€” Local mail filtering πŸ’Œ

okay bestie, buckle up, we're talking about localmailfilter(7), which is literally the way the Courier mail server decides whether it even wants your mail. no cap. it's giving gatekeeper energy and honestly? iconic. let's get into it 🫢


✨ SYNOPSIS ✨

like, here's the whole vibe in six little commands:

echo /usr/bin/maildrop >/etc/courier/maildropfilter

mkdir $HOME/.mailfilters

vi $HOME/.mailfilters/rcptfilter $HOME/.mailfilters/rcptfilter-ext

vi $HOME/.mailfilters/smtpfilter $HOME/.mailfilters/smtpfilter-ext

chmod 700 $HOME/.mailfilters

chmod 600 $HOME/.mailfilters/*

yeah that's it. that's the whole setup. slay πŸ’…


🌸 DESCRIPTION 🌸

so the maildrop mail filter can be used by the Courier mail server as a mail filtering engine, rejecting unwanted mail on a per-recipient basis. per-recipient!! she's picky and we love that for her πŸ’–

here's the tea though: the actual filtering interface Courier uses does not really require that maildrop be the engine. it's just that maildrop happens to have a compatible interface you can use right out of the box, no assembly required. the info below is enough for you to craft a homebrewed mail filter that takes maildrop's place. DIY queen behavior ✨

the local mail filter only works for addresses that correspond to local accounts. if the recipient is a remote address on another mail server? filtering is not used, not even a little. also, the local mail filter is disabled by default (she's shy πŸ™ˆ). to enable local mail filtering you'll need to initialize the /etc/courier/maildropfilter configuration file so it contains the pathname to your local mail filter.

local mail filtering happens in two distinct phases, and yes they are both main characters:

πŸ“¬ Recipient filters

when the Courier mail server receives an address naming a local mail recipient, the local mail recipient's mail filter is executed before the Courier mail server acknowledges the address. the local mail filter tells Courier whether to:

  • A) accept the message unconditionally β€” the message is whitelisted, she said yes πŸ’–
  • B) reject the message unconditionally β€” the Courier mail server tells the other mail server that the recipient address is invalid, blocked, cancelled πŸ’€
  • C) accept this recipient, but run the content mail filter once the message's contents are available β€” the "we'll see" option, very mysterious of her

πŸ“₯ Content filters

after receiving the contents of the message, the mail filter is executed again for any recipients whose recipient filters used that third option. the content filter can now actually examine the contents of the message and indicate whether the message should be accepted or rejected. and listen β€” content filtering is not available for alias addresses. sad but true 😭

quick note bestie: mail filtering is executed as an integral part of receiving a message from a remote mail server. if the message is rejected, the Courier mail server refuses to accept the message for delivery. periodt.

the local mail filter will be invoked like this:

HOME=$HOME FILTER -D uid/gid -M filter

and this part matters: the local mail filter will NOT be invoked as root. so if it needs to access files in the recipient's account, it must be installed setuid to root (which is how maildrop is installed by default). noted πŸ“

uid/gid is the recipient account's system userid and group id, respectively. the recipient account's home directory gets placed in the HOME environment variable prior to running FILTER, and filter is set as follows:

  • rcptfilter β€” the mail filter is invoked initially when the remote mail server specifies this address as a recipient. FILTER should terminate with one of these exit codes: 0 β€” this sender is acceptable; 99 β€” this sender is acceptable, but I want to run the content filter for this the message; any other non-zero exit code β€” the sender is not acceptable, reject the message.
  • smtpfilter β€” if FILTER terminates with exit code 99, FILTER runs again with this parameter set to the word smtpfilter. FILTER will be invoked once the message has been received from the remote mail server, but not yet acknowledged. if FILTER terminates with a non-zero exit code, the message is rejected. if FILTER terminated with the exit code of zero, the message is accepted.
  • rcptfilter-ext, smtpfilter-ext β€” if the recipient created sub-addresses β€” see dot-courier(5)[1] β€” a dash followed by the subaddress ext is appended to the name of the filter.
  • rcptfilter-alias-ext β€” this is how FILTER gets invoked if the address is a locally defined mail alias (ext is the alias name).

the rcptfilter invocation must terminate with a zero exit code when the message originates from a mailing list or any other source that should be considered as "whitelisted". here's why that's a whole thing: this filtering model does not fit very well with some mail transfer protocols, so unless trusted sources are explicitly declared to be whitelisted, there is a remote possibility that the recipient will be removed from a mailing list because of a poorly-written mail filter from some other recipient of the same message 😬. the 0 return exit code (which is the implied default if no mail filtering is installed) protects the recipient from being adversely affected, in any way, by anyone else's mail filter. she's protecting you fr fr 🫢

the mail filters may print a diagnostic message before rejecting a message. that diagnostic message will be returned to the sending mail relay, where possible. so like, say it with your chest πŸ’…

the mail filters inherit environment variables that describe the incoming mail. the following environment variables are provided by default:

  • SENDER β€” the return address on the message.
  • TCPREMOTEHOST, TCPREMOTEIP β€” when the message is received via ESMTP, these variables specify the remote IP address and the corresponding hostname. Hostname is empty if the IP address does not have a reverse DNS record, or is set to "softdnserr" if there was a temporary failure while looking up this IP address.
  • BLOCK2 β€” the default the Courier mail server configuration sets this environment variable if the remote IP address is listed in an unsecured relay blacklist. see /etc/courier/esmtpd for more information.

other environment variables may also be available. for mail received via ESMTP, environment variables are usually set in the /etc/courier/smtpaccess configuration file. iykyk ✨

🧚 maildrop implementation

maildrop implements this mail filtering API as follows:

  • $HOME/.mailfilters β€” this directory contains the filtering recipes. this directory, and its contents, cannot have any group or world permissions. lock it down, bestie πŸ”’
  • smtpfilter*, rcptfilter* β€” these mail filtering recipes directly correspond to the events defined in the previous section. maildrop's "import" statement can be used to gain access to the environment variables (these mail filters are executed in maildrop's embedded mode). the mail filtering recipes can set the EXITCODE variable appropriately before terminating, in order to accept or reject the message.

see maildrop(1)[2] for more information.

πŸ‘‘ Filtering mail to aliases

the /etc/courier/aliases configuration file is used to mail aliases, see makealiases(8)[3]. the system administrator may set aside a reserved local account that will be used to specify a local mail filter for messages addressed to aliases. the configuration file /etc/courier/aliasfilteracct specifies the home directory of the mail account that will be used to filter alias recipients.

for example, if /etc/courier/aliasfilteracct contains /home/admin, then the Courier mail server runs the mail filter as follows:

HOME=/home/admin FILTER -D uid/gid -M rcptfilter-alias-name

here, uid/gid is owner uid and gid of the specified directory. NOTE: name is a fully qualified address, and the local aliases listed in /etc/courier/aliases do not typically include the domain name. if [it] defines an alias called "system", for example, the -M option will probably be "system@example.com", if example.com is the contents of /etc/courier/me configuration file. honestly the domain thing trips everyone up, no shame 🌈

unfortunately, currently it is not possible to specify content filters (a.k.a. smtpfilters) for aliases, only recipient filters. crying, throwing up, etc. 😭


πŸ“ FILES πŸ“

  • /etc/courier/maildropfilter β€” Local mail filtering engine.
  • /etc/courier/aliasfilteracct β€” Account that is used to filter mail to aliases.

πŸ”— SEE ALSO πŸ”—

courierfilter(8)[4], maildrop(1)[2].


✍️ AUTHOR ✍️

Sam Varshavchik β€” Author


πŸ“Œ NOTES πŸ“Œ

  1. dot-courier(5) β€” https://www.courier-mta.org/dot-courier.html
  2. maildrop(1) β€” https://www.courier-mta.org/maildrop.html
  3. makealiases(8) β€” https://www.courier-mta.org/makealiases.html
  4. courierfilter(8) β€” https://www.courier-mta.org/courierfilter.html

and that's a wrap on localmailfilter(7), from the Courier Mail Server, dated 01/10/2026. go forth and filter responsibly, you absolute legend πŸ’–βœ¨πŸŽ€πŸ¦‹

mwah πŸ’‹

πŸ“¦ where this page comes from
  • ubuntu stonking: courier-mta 2.0.6-1 Β· GPL-3+~with-OpenSSL-exception
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms