Send email with chrooted PHP in OpenBSD

OpenBSD highly enables chrooting daemons. I try to do it as much as possible. But lazy software sometime fail to work out of the box. Here’s my notes to enable sending email via chroot PHP (in my case, hear WordPress).

I use Apache + PHP-FPM. Those directions might work when using Apache + mod_php.

The PHP-FPM is configured for chroot:

# vi /etc/php-fpm.conf
user = www
group = www
chroot = /var/www

Configure PHP to use the right mail software, namely femail. And setup the chroot:

# vi /etc/php-7.0.ini
sendmail_path = "/bin/femail -t -i -f"

# mkdir /var/www/etc ; cp -p /etc/resolv.conf /var/www/etc/
# cp -p /bin/sh /var/www/bin/

Restart the PHP-FPM daemon:

# rcctl restart php70_fpm

A test can be done on the command line:

# echo "This is a test mail" | chroot -g www -u www /var/www /bin/femail -v -t -i -f
<<< 220 ESMTP OpenSMTPD
>>> EHLO
<<< Hello [], pleased to meet you
<<< 250-8BITMIME
<<< 250-SIZE 36700160
<<< 250-DSN
<<< 250 HELP
>>> MAIL FROM:<>
<<< 250 2.0.0: Ok
>>> RCPT TO:<>
<<< 250 2.1.5 Destination address valid: Recipient ok
>>> DATA
<<< 354 Enter mail, end with "." on a line by itself
<<< 250 2.0.0: 5aa5e8be Message accepted for delivery
>>> QUIT

A test can also be done using a PHP file available from the web server:

# cat /var/www/htdocs/test_phpmail.php
  # Test PHP for sending email
  $message = "This is a test message.";
  $result = mail('', 'Test message', $message);
  echo "Result: $result";

If both mail are received, you’re good.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


%d bloggers like this: