3

Objective

I'm trying to configure a CentOS 7 system so all mail sent to local system accounts gets redirected to an alternate email address. For example, if mail is sent to root I want it to be redirected to [email protected].

I have a mostly working configuration using postfix, generic maps, and aliases. If the complete configuration I'm using will help, ask and I'll modify the question to include it.

If there's a better way of accomplishing my objective, please tell me. I'd prefer to stick with postfix, but only because it's the default MTA on CentOS.

Problem

I'm trying to configure postfix as a smart host so I can relay mail via a ZOHO account. However, ZOHO is extremely picky about the format of the from address.

When I try to send mail with a sender address that looks like this, it works:

[email protected]

However, when I try to send mail with a sender address that looks like this, it fails:

[email protected] (Ryan)

Examples

Below are 4 examples sent using the exact same postfix configuration.

Allowed

I use this command:

mail -r "[email protected]" -s "Testing." root <<< "Test to root."

...and see this in maillog (I've stripped the message down and reformatted it to make it a bit more readable):

CD9FA29D20: from=<[email protected]>
CD9FA29D20: to=<[email protected]>,
    orig_to=<root>,
    relay=smtp.zoho.com[74.201.154.90]:587,
    delay=2.4,
    delays=0/0.02/1.7/0.66, dsn=2.0.0,
    status=sent (250 Message received)

Allowed

I use this command:

mail -r "Ryan <[email protected]>" -s "Testing." root <<< "Test to root."

...and see this in maillog (I've stripped the message down and reformatted it to make it a bit more readable):

936C929D20: from=<[email protected]>
936C929D20: to=<[email protected]>,
    orig_to=<root>,
    relay=smtp.zoho.com[74.201.154.90]:587,
    delay=2.3,
    delays=0/0.03/1.6/0.7,
    dsn=2.0.0,
    status=sent (250 Message received)

Relaying Denied

I use this command:

mail -r "[email protected] (Ryan)" -s "Testing." root <<< "Test to root."

...and see this in maillog (I've stripped the message down and reformatted it to make it a bit more readable):

D57C529D20: from=<[email protected]>
D57C529D20: to=<[email protected]>,
    orig_to=<root>,
    relay=smtp.zoho.com[74.201.154.90]:587,
    delay=2,
    delays=0.01/0/1.4/0.64,
    dsn=5.0.0,
    status=bounced (host smtp.zoho.com[74.201.154.90] said:
        553 Relaying disallowed as [email protected] (Ryan) (in reply to end of DATA command))

Relaying Denied

I use this command:

mail -s "Testing." root <<< "Test to root."

...and see this in maillog (I've stripped the message down and reformatted it to make it a bit more readable):

9CFE629D20: from=<[email protected]>
9CFE629D20: to=<[email protected]>,
    orig_to=<root>,
    relay=smtp.zoho.com[74.201.154.90]:587,
    delay=2,
    delays=0/0.02/1.4/0.6,
    dsn=5.0.0,
    status=bounced (host smtp.zoho.com[74.201.154.90] said:
        553 Relaying disallowed as [email protected] (Ryan) (in reply to end of DATA command))

Question

It's the last example that I'm having trouble with since the default when not explicitly including the address is to write the address in a way that ZOHO will reject.

Is it possible to have postfix strip or rewrite a person's name when rewriting a sender address? There's a similar question to here, but it doesn't accomplish what I want. I don't want to rely on changing user account settings to ensure my mail doesn't get bounced.

3
  • 2
    The normally accepted format for the from address is Ryan <[email protected]>. Aug 29, 2015 at 17:27
  • @MichaelHampton Yes, I forgot to mention that I'm able to use that format successfully. I added an example to show that format works. I also added an example that shows how omitting the sender address using mail will cause it to be written the way that ZOHO rejects. Maybe there's some mail configuration that I've missed.
    – Ryan J
    Aug 29, 2015 at 17:39
  • 1
    In that case I would check your ~/.mailrc and environment variables.
    – HBruijn
    Aug 29, 2015 at 17:44

2 Answers 2

4

The key to solving this is understanding how the postfix cleanup process handles missing header information. From the cleanup man page:

The cleanup(8) daemon always performs the following transformations:

   o      Insert missing  message  headers:  (Resent-)  From:,  To:,  Mes-
          sage-Id:, and Date:.

An old mailing list post give's a hint about the formatting of inserted From headers:

The defaults are:

MAIL FROM address = UNIX login name
FROM: header = UNIX login name (GECOS information)

Since it's the header information that needs to be changed or reformatted, using postfix's header_checks seems like the obvious solution. However, it doesn't work. Reading the header_checks man page there's another hint in the BUGS section.

Message headers added by the cleanup(8) daemon itself are excluded from
inspection.  Examples  of  such  message  headers  are From:, To:, Mes-
sage-ID:, Date:.

There's another mailing list thread with someone else trying to solve the same problem and they end up using smtp_header_checks. There isn't a lot of documentation for smtp_header_checks, but, since the processing is applied when mail is delivered via SMTP, it can be used to rewrite the From header added by cleanup.

Here's an example of an /etc/postfix/smtp_header_checks configuration that will change the From (header) address of every outgoing message to LOCALHOST System <[email protected]>:

/^From:.*/ REPLACE From: LOCALHOST System <[email protected]>

Please note I'm not a regex expert, so you may want to test your own regex rather than relying on the one above.

After adding smtp_header_checks, the following config option must be added to /etc/postfix/main.cf:

smtp_header_checks = pcre:/etc/postfix/smtp_header_checks

Then reload the postfix config (assuming systemd) and send a test message:

sudo systemctl reload postfix
sendmail [email protected] << EOF
Subject: Test email to root.

Testing.
EOF
3

It is fairly common that mail servers require a valid e-mail address and reject invalid ones.

 [email protected] (Ryan)

is not a valid e-mail address. The wikipedia entry has some useful pointers, as well as references to the relevant RFC's.

In practice you use either a bare email address:

[email protected]

or whatever display name followed by a mailbox enclosed with <> such as:

Ryan <[email protected]> 
2
  • The problem I'm having is that mail (heirloom-mailx) will use the incorrect format for the from address if I don't explicitly add it with -r. I added a couple more examples to my question to make it more clear.
    – Ryan J
    Aug 29, 2015 at 17:53
  • That erroneous from address probably gets picked up from either your environment variables (in bash check with env and/or set) or your ~/.mailrc rather than your postfix, although postfix can be modified to do a quite a bit of rewriting too.
    – HBruijn
    Aug 29, 2015 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .