Set Sender/Reply-To for sendmail in PHP

Today I tried to configure the sender for sendmail in php. There are some ways how to do it, so I share my solution.

There is a sendmail_from setting in php cli and php fpm, but it is usually empty. So sendmail fallsback to user@hostname (given you are logged in as user and the machine is called hostname).

But I found a better way (maybe even easier to extend). Just change the sendmail_path to something with an extra -f noreply@example.org at the end.

The config in php.ini looks like this:

sendmail_path = /usr/sbin/sendmail -t -i -f noreply@example.org

If you want to change it in PHP-FPM, you can do this for each pool (e.g. /etc/php5/pool.d/www.conf) by appending:

php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f noreply@example.org

Dont forget to restart php-fpm after this change!

That's it.

As a small bonus: If you want to send a mail from commandline/bash with a proper sender address, it works like this:

$ echo "The mail body" | mail -s "The Subject" hans@example.org -- -f noreply@example.org
# sends mail to hans@example.org from noreply@example.org

In open source, php, sendmail, ubuntu by @ 09 Jul 2014

comments powered by Disqus

Recent Files


Recent Dev-Articles

Read recently