Mailing List Archive

Re-writing usernames
I have a need to re-write the From: header on outgoing emails such that
email address like john@nlc.net.au get re-written to john.saunders@nlc.net.au
automatically. I have always had incoming message being accepted as both
john and john.saunders using the /var/qmail/users/mailnames file. After
reading about environment variables in qmail-inject I came up with the
following script. This script is installed in /usr/lib and/or /usr/sbin
in preference to the symlink pointing to qmail's sendmail clone.

Note that if your /var/qmail/users/mailnames file is in a consistent
format you can use the same config file for outgoing re-writing as is
used by qmail for incoming messages. See the 2 commented out lines for
how to enable this if your /var/qmail/users/mailnames file looks like
lines of the form "login:login:mailname".

#!/usr/local/bin/perl
#
# This program re-writes outgoing email names. The map file is assumed to
# contain lines of the form "login:mailname". Be sure to setup the inverse
# mappings using /usr/qmail/users/mailnames (or qmsmac) so that the re-
# written address can be replied to.
#

$mapfile = "/var/qmail/users/mailnames.rewrite";
#$mapfile = "/var/qmail/users/mailnames";

# Only do anything is the mapfile can be opened for reading.
if (open(MAP, $mapfile))
{
# Find out who is running sendmail.
($username, $pw, $uid, $gid, $quota, $comment, $fullname) = getpwuid($<);

# Read all lines from the nam file.
while (<MAP>)
{
# Extract both partsfrom the mapfile line.
chop;
($name, $rewrite) = split(/:/);
#($name, $dupname, $rewrite) = split(/:/);

# Check for a match.
if ($name == $username)
{
# Setup the environment so that qmail-inject will re-write.
$ENV{"QMAILUSER"} = $rewrite;
$ENV{"QMAILNAME"} = $fullname;
$ENV{"QMAILINJECT"} = "f";
last;
}
}
}

# Pass control to qmail's sendmail clone.
exec("/var/qmail/bin/sendmail", @ARGV);

-- +------------------------------------------------------------+
. | John Saunders - mailto:john@nlc.net.au (EMail) |
,--_|\ | - http://www.nlc.net.au/ (WWW) |
/ Oz \ | - 018-223-814 or 02-9477-2881 (Phone) |
\_,--\_/ | NHJ NORTHLINK COMMUNICATIONS - Supplying a professional, |
v | and above all friendly, internet connection service. |
+------------------------------------------------------------+
Re: Re-writing usernames [ In reply to ]
I was just a tad too early on the send button. Patch level 1 is out. :(

John Saunders (john.saunders@nlc.net.au) wrote:
> # Check for a match.
> if ($name == $username)
^^
Change "==" to "eq" as it's a string compare that's needed.

Cheers.
-- +------------------------------------------------------------+
. | John Saunders - mailto:john@nlc.net.au (EMail) |
,--_|\ | - http://www.nlc.net.au/ (WWW) |
/ Oz \ | - 018-223-814 or 02-9477-2881 (Phone) |
\_,--\_/ | NHJ NORTHLINK COMMUNICATIONS - Supplying a professional, |
v | and above all friendly, internet connection service. |
+------------------------------------------------------------+