Centos – How to strip or rewrite the sender’s name of a from address when using postfix

centospostfix

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 sysadmins@example.com.

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:

noreply@example.com

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

noreply@example.com (Ryan)

Examples

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

Allowed

I use this command:

mail -r "noreply@example.com" -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=<noreply@example.com>
CD9FA29D20: to=<sysadmins@example.com>,
    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 <noreply@example.com>" -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=<noreply@example.com>
936C929D20: to=<sysadmins@example.com>,
    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 "noreply@example.com (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=<noreply@example.com>
D57C529D20: to=<sysadmins@example.com>,
    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 noreply@example.com (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=<ryan@localhost.localnet>
9CFE629D20: to=<sysadmins@example.com>,
    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 noreply@example.com (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.

Best Answer

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

 noreply@example.com (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:

noreply@example.com

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

Ryan <noreply@example.com>