Sending emails from web applications like PHP may result in rejecting the emails from some servers. Fighting spam emails results in too strict filters and rules, which reject the mails even before the anti-spam service of the accepting server. Here is an error:
Apr 1 04:10:18 srv-mail postfix/pickup: AB13578FAB3: uid=1015 from=<www-data> Apr 1 04:10:18 srv-mail postfix/cleanup: AB13578FAB3: message-id=<20200401041018.AB13578FAB3@www.mydomain.com> Apr 1 04:10:18 srv-mail postfix/qmgr: AB13578FAB3: from=<email@example.com>, size=7923, nrcpt=1 (queue active) Apr 1 04:10:19 srv-mail postfix/smtp: AB13578FAB3: to=<firstname.lastname@example.org>, relay=mx.example.com[126.96.36.199]:25, delay=11, delays=0.02/0.01/0.65/10, dsn=5.0.0, status=bounced (host mx.example.com[188.8.131.52] said: 550-Sender verification is required but failed. (ID:550:0:5 550 (smtp1.mx.example.com)): email@example.com (in reply to MAIL FROM command))
The receiving server has too strict rules!
It just expects the “From” and the “Return-Path” headers to contain the same string – the sender’s email box.
As you can see, from the example above, the application sends all emails (from let’s say web forms) from the firstname.lastname@example.org and probably the www-data is the username of the OS user, under which the application executes.
Or you want to overwrite the Return-Path because it uses the username of the application, which sent the email like “web”, “apache”, “www-data” and so on.
Here is how to overwrite the Return-Path with postfix mail system.
STEP 1) Edit postfix configuration
Add a line in /etc/postfix/main.cf (it is perfectly fine to be on the last line):
smtp_generic_maps = hash:/etc/postfix/generic
And create the file /etc/postfix/generic with mapping “email@example.com firstname.lastname@example.org”:
The domains of the emails may be different or the same. It doesn’t matter. If you do not know what is your “email@example.com” the mail logs in /var/log/messages or /varlog/mail maight help to find the emailbox or just send yourself an email and look for the Return-Path.
And a real-world example for /etc/postfix/generic
STEP 2) Generate the hash file, which postfix will use. Reload the postfix.
The postfix will use the hash file add in the configuration. Just execute:
The above command will create a binary file /etc/postfix/generic.db, which will be used by the postfix mail system. Do not edit the file directly. To add entry, just use a text editor and edit /etc/postfix/generic (without the “.db” suffix) and then reload/restart the postfix to enable the new configuration.
And reload (or restart) postfix with
systemctl reload postfix
or for init systems: