Skip to content

9. Configuring Email Functionality

9.1. Introduction to Server Email Configuration

Section titled “9.1. Introduction to Server Email Configuration”

Most web applications, including WordPress, need to send emails for various purposes such as password resets, notifications, and contact form submissions. In this section, we’ll configure MSMTP, a lightweight SMTP client that allows your server to send emails through an external SMTP server.

First, let’s install the necessary packages:

# Update package lists
sudo apt update
# Install MSMTP and the MTA (Mail Transfer Agent) wrapper
sudo apt install msmtp msmtp-mta

The msmtp-mta package allows system programs to use MSMTP as the default mail sender.

Create a configuration file in your home directory:

# Navigate to your home directory
cd
# Create and edit the configuration file
nano .msmtprc

Add the following configuration to your .msmtprc file, replacing the placeholder values with your actual email service details:

# Global defaults
defaults
# TLS settings
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# Logging
logfile ~/.msmtp.log
# Account configuration
account mymail
# SMTP server settings
host smtp.example.com
port 587
auth on
user your_email@example.com
password your_app_password
from your_email@example.com
# Set default account
account default : mymail

Since the configuration file contains your email password, it’s crucial to set proper permissions:

# Set restrictive permissions on the config file
sudo chmod 600 ~/.msmtprc

Create a log file to track email sending activity:

# Create the log file
touch ~/.msmtp.log
# Set appropriate ownership and permissions
sudo chown $USER:$USER ~/.msmtp.log
sudo chmod 660 ~/.msmtp.log

Let’s verify that MSMTP is working correctly by sending a test email:

# Send a test email (replace with recipient's email)
msmtp recipient@example.com

After running this command:

  1. Type a subject line and press Enter
  2. Type your message body
  3. Press Ctrl+D to send the email

If everything is configured correctly, the email should be sent without any error messages.

To allow PHP applications (like WordPress) to send emails, we need to configure MSMTP system-wide.

9.5.1. Creating a System-Wide Configuration

Section titled “9.5.1. Creating a System-Wide Configuration”
# Copy your user configuration to the system location
sudo cp ~/.msmtprc /etc/msmtprc
# Set appropriate ownership and permissions
sudo chown www-data:www-data /etc/msmtprc
sudo chmod 660 /etc/msmtprc

Edit the system-wide configuration file to use a different log location:

# Edit the system configuration
sudo nano /etc/msmtprc

Change the log file path to:

logfile /var/log/msmtp.log
# Navigate to the log directory
cd /var/log/
# Create the log file
sudo touch msmtp.log
# Set appropriate ownership and permissions
sudo chown www-data:adm msmtp.log
sudo chmod 640 msmtp.log

Create a PHP script to test email sending:

# Navigate to your home directory
cd
# Create a test PHP file
nano php_mail_test.php

Add the following content to the file, replacing the email addresses with your actual addresses:

<?php
// Enable error reporting for debugging
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Email details
$from = "your_email@example.com"; // Must match the 'from' in msmtp config
$to = "recipient@example.com";
$subject = "PHP Mail Test";
$message = "This is a test email sent from PHP using MSMTP.";
$headers = "From: " . $from;
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo "Test email sent successfully!";
} else {
echo "Email sending failed.";
}
?>

9.6.1. Running the Test as the Web Server User

Section titled “9.6.1. Running the Test as the Web Server User”

To properly test the email functionality as it would be used by your web applications:

# Run the PHP script as the www-data user
sudo -u www-data php php_mail_test.php

If successful, you should see “Test email sent successfully!” and the email should arrive in the recipient’s inbox.

If you encounter issues with email sending:

  1. Check the log files:

    cat ~/.msmtp.log # For user-level issues
    sudo cat /var/log/msmtp.log # For system/PHP issues
  2. Verify SMTP settings:

    • Confirm your SMTP server address, port, and credentials
    • For Gmail, ensure you’re using an App Password if 2FA is enabled
  3. Test connectivity:

    telnet smtp.example.com 587

    If you can connect, you should see a response from the server

  4. Check PHP mail configuration:

    php -i | grep mail

    Ensure PHP is configured to use the system’s sendmail (which is now MSMTP)

  • Never use your primary email password; use app-specific passwords when possible
  • Regularly rotate your email credentials
  • Consider using environment variables or a secure vault for storing credentials in production environments
  • Remove or secure test files after confirming functionality:
    rm ~/php_mail_test.php

With MSMTP properly configured, your server can now send emails through your specified SMTP provider, enabling essential functionality for WordPress and other applications.