Search Our Database

How to set up SMTP send mail using PHPMailer on a Linux server

Last updated on |
under |

Introduction

This guide is intended for web administrators or developers using a Linux server to set up SMTP email sending with PHPMailer. PHPMailer is a popular PHP library that simplifies sending emails via SMTP. This guide will walk you through the necessary steps to install PHPMailer, configure it on your server, and send test emails using SMTP. We will cover the specific commands and directories used on Linux-based systems.

 

Prerequisites

  • Access to a Linux server with a web server installed (e.g., Apache or Nginx)
  • PHP installed on the server
  • SMTP server details (SMTP host, port, username, and password)
  • Basic knowledge of Linux command-line operations
  • Access to the server via SSH

 

Steps to set up SMTP send mail using PHPMailer

1. Download PHPMailer using wget

First, connect to your server via SSH. Use the following command to download the PHPMailer source code directly from the GitHub repository:

wget https://github.com/PHPMailer/PHPMailer/archive/refs/heads/master.zip

2. Unzip the downloaded file

After downloading the ZIP file, unzip it using the following command:

unzip master.zip

This will extract the contents of the ZIP file into a directory named PHPMailer-master.

 

3. Move the PHPMailer files to your web root directory

Depending on your server configuration, the web root directory might vary. For Apache on Ubuntu, the default web root directory is usually/var/www/html/ , and for DirectAdmin users, it is typically /home/<username>/public_html. You can use the following command to move the PHPMailer files into the web root directory:

mv PHPMailer-master /var/www/html/

Or, for DirectAdmin:

mv PHPMailer-master /home/<username>/public_html/
Important Note: Find the correct web root directory if you’re using httpd

If you’re not sure where your web root directory is, and you’re using httpd, you can run the following command to find the web root using grep:

httpd -S | grep "DocumentRoot"

This will display the location of your DocumentRoot for all virtual hosts. Use the output to determine where to move the PHPMailer files.

 

4. Create a mail-sending PHP script

Create a new PHP file in the web root directory (e.g., /var/www/html/sendmail.php or /home/<username>/public_html/sendmail.php) with the following code. Modify the placeholders with your SMTP details, email addresses, and content:

<?php
require("/var/www/html/PHPMailer-master/PHPMailerAutoload.php");

$mail = new PHPMailer();

$mail->IsSMTP(); // Send via SMTP
$mail->Host = "127.0.0.1"; // Change to your SMTP server or localhost
$mail->Port = 2525; // Change SMTP port if needed
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "email displayed name"; // SMTP username
$mail->Password = "password"; // SMTP password

$mail->From = "email@domain.com"; // Your email
$mail->FromName = "Your Name"; // Sender's name
$mail->AddAddress("Recipient@emailaddress.com", "Recipient Name"); // Recipient
$mail->AddReplyTo("yourname@domain.com", "Your Name"); // Reply-To address

$mail->WordWrap = 50; // Word wrapping
$mail->IsHTML(true); // Send as HTML

$mail->Subject = "Here is the subject"; // Email subject
$mail->Body = "This is the HTML body"; // HTML message body
$mail->AltBody = "This is the text-only body"; // Fallback text-only body

if(!$mail->Send()) {
    echo "Message was not sent";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

echo "Message has been sent";
?>

 

5. Test the script

After configuring the script with your SMTP details and email content, navigate to the location of the PHP file in your browser (e.g.. http://yourdomain.com/sendmail.php). If everything is configured correctly, the script will send an email to the specified recipient.

 

Conclusion

You have now successfully configured SMTP email sending with PHPMailer on your Linux server. This setup ensures secure and reliable email delivery through your server. For further configuration options or troubleshooting, you can refer to the PHPMailer documentation.

For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.

Article posted on 21 March 2020 by Louis