Setup PHP for Email using sSMTP

Post Reply
User avatar
LHammonds
Site Admin
Site Admin
Posts: 712
Joined: Fri Jul 31, 2009 6:27 pm
Are you a filthy spam bot?: No
Location: Behind You
Contact:

Setup PHP for Email using sSMTP

Post: # 696Post LHammonds
Mon Feb 04, 2019 11:38 am

Email Overview

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.

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

apt -y install ssmtp
Backup the original configuration files:

Code: Select all

cp /etc/ssmtp/ssmtp.conf /etc/ssmtp/ssmtp.bak
cp /etc/ssmtp/revaliases /etc/ssmtp/revaliases.bak
Edit the main configuration file:

Code: Select all

vi /etc/ssmtp/ssmtp.conf
Set the following (make sure to use your settings):

Code: Select all

root=
mailhub=192.168.107.25:25
hostname=localhost
Edit the reverse alias file:

Code: Select all

vi /etc/ssmtp/revaliases
Add the alias for "root" to be that of your desired "FROM" address. (make sure to use your settings) Modify PHP.ini for Apache

Edit the Apache PHP.ini

Code: Select all

vi /etc/php/7.2/apache2/php.ini
Set the sendmail_path variable to point to the default sendmail path (which is a link to ssmtp)

Code: Select all

sendmail_path = /usr/sbin/sendmail -t
Or set the sendmail_path variable to point to ssmtp directly:

Code: Select all

sendmail_path = /usr/sbin/ssmtp -t
Modify PHP.ini for Command-Line

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

vi /etc/php/7.2/cli/php.ini
Make the same changes here that you did above for Apache's PHP.ini

Send Test Email

Create 2 PHP test scripts to send an email message from a web browser and the command line.

Code: Select all

touch /var/www/html/testmail-web.php
touch /var/www/html/testmail-cli.php
chown www-data:root /var/www/html/testmail-*.php
chmod 644 /var/www/html/testmail-*.php
Here is the code to add into the web test: (be sure to change the TO and FROM addresses to reflect your environment)

Code: Select all

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>
Here is the code to add into the command-line interface (cli) test: (be sure to change the TO and FROM addresses to reflect your environment)

Code: Select all

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";
?>
Test from the command-line:

Code: Select all

php /var/www/html/testmail-cli.php
Now test from a browser:

Code: Select all

http://mydomain.com/testmail-web.php
If you have any issues, check /var/log/syslog for messages related to sSMTP

Remove the temporary PHP test scripts:

Code: Select all

rm /var/www/html/testmail-*.php
Web Application Email Settings

Enable email however it is done in your application.

Post Reply