Many PHP web applications utilize PHP's built-in "mail" function which means we need to configure php.ini to use a mail client that will send email. System scripts like fail2ban can also make use of command-line email.
It seems like an industry standard to use "sendmail" based on all the tutorials out there but sendmail is a mail server itself which can receive mail which is overkill for our purposes. We only want to send outbound to a dedicated mail server...not turn each web server into a mail server.
For this reason, we will install a lightweight mail client called ssmtp that will allow sending outbound-only mail messages.
We will then modify PHP to utilize this mail client. The final step would be to configure the web application to send mail.
Mail Server Information
There are many scenarios surrounding how one might have a mail service.
For this tutorial, let's assume you have a mail server that you configured to act as a relay server for this specific server...which means you can send email to that server to be delivered locally or routed to another mail server if the destination is not local. Let's assume this relay server will not require login credentials but does require the FROM address to be specific. Let's also assume it does not require encryption.
Here are the settings used for this example: (be sure to change them throughout this article to match your environment)
Mail Relay Server Address: 192.168.107.25
Mail Relay Server Port: 25
Use TLS? No
From Address: webmaster@mydomain.com
To Address: YourEmailAddr@mydomain.com
Install and Configure SSMTP
Install the mail client:
Code: Select all
sudo apt -y install ssmtp
Code: Select all
sudo cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.bak
sudo cp /etc/ssmtp/revaliases /etc/ssmtp/revaliases.bak
Code: Select all
sudo vi /etc/ssmtp/ssmtp.conf
Code: Select all
root=
mailhub=192.168.107.25:25
hostname=localhost
FromLineOverride=YES
UseSTARTTLS=NO
#AuthUser=user@mydomain.com
#AuthPass=userpassword
Edit the reverse alias file:
Code: Select all
sudo vi /etc/ssmtp/revaliases
Code: Select all
root:webmaster@mydomain.com
Edit the Apache PHP.ini
Code: Select all
sudo vi /etc/php/7.2/apache2/php.ini
Code: Select all
sendmail_path = /usr/sbin/sendmail -t
Code: Select all
sendmail_path = /usr/sbin/ssmtp -t
While we are making the mail command work for PHP inside Apache, we might as well do the same for the command-line interface.
Make the same changes above to this file as well.
Code: Select all
sudo vi /etc/php/7.2/cli/php.ini
Send PHP Test Email
Create 2 PHP test scripts to send an email message from a web browser and the command line.
Code: Select all
sudo touch /var/www/html/testmail-web.php
sudo touch /var/www/html/testmail-cli.php
sudo chown www-data:root /var/www/html/testmail-*.php
sudo chmod 644 /var/www/html/testmail-*.php
Code: Select all
sudo vi /var/www/html/testmail-web.php
Code: Select all
<html>
<head>
<title>Test mail</title>
</head>
<body>
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "webmaster@mydomain.com";
$to = "YourEmailAddr@mydomain.com";
$subject = "PHP Mail Test script - Web";
$message = "This is a test to check the PHP Mail functionality from a web browser";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "<p>Test email sent</p>";
?>
</body>
</html>
Code: Select all
sudo vi /var/www/html/testmail-cli.php
Code: Select all
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "webmaster@mydomain.com";
$to = "YourEmailAddr@mydomain.com";
$subject = "PHP Mail Test script - cli";
$message = "This is a test to check the PHP Mail functionality from command line";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
Code: Select all
php /var/www/html/testmail-cli.php
Code: Select all
http://mydomain.com/testmail-web.php
Remove the temporary PHP test scripts:
Code: Select all
sudo rm /var/www/html/testmail-*.php
Modify the recipient email address below and run the commands on the console and see if you received the test email:
Code: Select all
echo "To: recipient@mydomain.com" > /tmp/mail.txt
echo "Subject: Testing 1-2-3" >> /tmp/mail.txt
echo " " >> /tmp/mail.txt
echo "This is a test, this is only a test. If this had been a real emergency, I would not be sending this email" >> /tmp/mail.txt
/usr/sbin/ssmtp -t < /tmp/mail.txt
rm /tmp/mail.txt
Modify the recipient email address below and run the commands on the console and see if you received the test email:
Code: Select all
echo "To: recipient@mydomain.com" > /tmp/mail.txt
echo "From: AlternateName<device@mydomain.com" >> /tmp/mail.txt
echo "Subject: Testing 1-2-3 in HTML" >> /tmp/mail.txt
echo "Mime-Version: 1.0" >> /tmp/mail.txt
echo "Content-type: text/html; charset="“iso-8859-1”"" >> /tmp/mail.txt
echo " " >> /tmp/mail.txt
echo "<h1>This is a test, this is only a test.</h1><h2>If this had been a real emergency, I would not be sending this email</h2>" >> /tmp/mail.txt
echo "<img src='https://www.w3.org/WAI/tutorials/img/w3c-796023c4.png' />" >> /tmp/mail.txt
/usr/sbin/ssmtp -t < /tmp/mail.txt
rm /tmp/mail.txt
Web Application Email Settings
Enable email support however it is done in your application.