Qmail recipes 1: Forwarding mail and changing the envelope sender
So, I figured out a few things about qmail, vpopmail and spamassassin recently and this post will kick off a small series of the more curious things I learned. This will not be a series of "setting up qmail for standard mail" how-tos, because enough of those exist already. If you need help with setting up qmail start here, but instead of netqmail, I'd recommend John Simpson's combined qmail patch and his service-qmail-smtpd-run script. So here's the first recipe!
How do I forward emails from an account and change the envelope sender?
If you want to forward bob@test.com's emails to alice@example.com, you just put "alice@example.com" into /home/bob/.qmail and /home/bob/.qmail-default and that's it, right?
A small cave-at of this technique is that it retains the original email's envelope sender. So Bob's email addresses (bob and bob-ext) are now permanently redirected to Alice, but if someone still sends mail from Bob's email-account, Alice might receive a bounce. If that happens the forwarding might break, because in qmail's case, the envelope sender will be "#@[]" (a null-envelope sender is explicitly allowed in RFC 2821, it was intended for bounces and such). However, simple spam-checkers, like the MFCHECK-patch for qmail, will refuse envelope senders without a valid MX record. So the bounce will never get through to Alice.
As it turns out, you can avoid qmail-local's forwarding in a .qmail file and pipe the message directly into qmail-inject, providing an explicit envelope sender using the QMAILSUSER and QMAILSDOMAIN environment variables like this:
|env QMAILSUSER='forward' QMAILSDOMAIN='test.com' \ /var/qmail/bin/qmail-inject -a alice@example.com
The "-a" parameter is important, because it prevents qmail-inject from resending the forwarded mail to everybody listed in the email's "To:", "Cc:" and "Bcc:" headers. By using "-a" the email will only be sent to Alice.
Usually qmail-local would put the forwarded mail directly into the queue again, by bypassing this and going through qmail-inject we get more control over that process.
My qmail recipes:
03/02/2008: The second recipe is online here: Integrating qmail with nixspam and spamassassin



02/15/2008 at 09:53 #
[...] from the maurus.net weblog is writing down some qmail recipies. The first entry is about “Forwarding mail and changing the envelope sender”. I guess for everyone who is interested in qmail and working with it, there will be some helpful [...]