Send email with chrooted PHP in OpenBSD
270 words, 2 minutes
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 www@tumfatig.net"
# 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 www@tumfatig.net test@email.com
<<< 220 tumfatig.net ESMTP OpenSMTPD
>>> EHLO cherie.tumfatig.net
<<< 250-tumfatig.net Hello cherie.tumfatig.net [127.0.0.1], pleased to meet you
<<< 250-8BITMIME
<<< 250-ENHANCEDSTATUSCODES
<<< 250-SIZE 36700160
<<< 250-DSN
<<< 250 HELP
>>> MAIL FROM:<www@tumfatig.net>
<<< 250 2.0.0: Ok
>>> RCPT TO:<test@email.com>
<<< 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
<?php
# Test PHP for sending email
$message = "This is a test message.";
$result = mail('test@email.com', 'Test message', $message);
echo "Result: $result";
?>
If both mail are received, you’re good.