How to send email with PHPMailer via SMTP in cPanel

Published Updated

To send email from PHP reliably, use PHPMailer to log in to a mailbox on your domain over SMTP instead of the PHP mail() function.

Many developers send email with the built-in PHP mail() function, but those messages often never arrive. The headers that mail() creates lack proper authentication, so the server may not relay the message, and providers like Gmail treat it as spam or discard it. SMTP with a real mailbox login gives the message the authentication headers it needs for delivery.

This guide sends a test message with the PHPMailer library. When the test works, you can reuse the same code in your own application.

Before you begin

  • A mailbox on your domain, such as you@example.com, and its password. PHPMailer uses this mailbox to log in to the SMTP server. To create one, see How to create a mailbox.
  • Your cPanel username and password, for SSH access.
  • An SSH client (Terminal on macOS and Linux, or PowerShell on Windows) or use Terminal function in cPanel.

Connect to your hosting account with SSH

SSH (a secure command-line connection to your server) lets you download PHPMailer and create the test file directly in your hosting account. Use your domain name as the server, your cPanel login details.

Download PHPMailer

In your SSH session, create a test folder inside public_html and download PHPMailer into it with Git.

  1. Create the folder and open it:
cd ~/public_html
mkdir test
cd test
  1. Clone the PHPMailer repository:
git clone https://github.com/PHPMailer/PHPMailer.git

Git creates a folder named PHPMailer inside test.

Create the PHPMailer test file

In your SSH session, inside the public_html/test folder, create a PHP file that sends one message through your mailbox.

  1. Open a new file in the nano editor:
nano test.php
  1. Paste the following code into the file:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);

try {
    // SMTP server settings
    $mail->isSMTP();
    $mail->Host       = 'mail.example.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'you@example.com';   // full email address
    $mail->Password   = 'your-password';     // mailbox password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port       = 587;
    // $mail->SMTPDebug = SMTP::DEBUG_SERVER; // remove // to see the SMTP conversation

    // Sender and recipient
    $mail->setFrom('you@example.com', 'Your Name');
    $mail->addAddress('recipient@gmail.com', 'Recipient Name');
    $mail->addReplyTo('you@example.com', 'Your Name');

    // Message
    $mail->isHTML(true);
    $mail->Subject = 'PHPMailer SMTP test';
    $mail->Body    = 'This is the HTML message body <b>in bold</b>.';
    $mail->AltBody = 'This is the plain-text body for mail apps that do not show HTML.';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

Replace example.com with your domain, you@example.com and your-password with your mailbox address and password, and recipient@gmail.com with an address where you can check the test message.

  1. Press Ctrl+O, then Enter to save the file.
  2. Press Ctrl+X to close nano.

Run the test and check the result

  1. Open https://example.com/test/test.php in your browser, with your domain instead of example.com.
  2. Check the page. It shows Message has been sent when the email leaves the server.
  3. Open the recipient mailbox and confirm the test message arrived.

If the page shows Message could not be sent, the text after Mailer Error tells you why. See the troubleshooting section below.

Delete the test file after testing

Warning: test.php contains your mailbox password in plain text, and anyone who opens its URL sends an email from your account. Delete the test folder as soon as the test works.

In your SSH session, run:

rm -rf ~/public_html/test

Troubleshooting

SMTP Error: Could not authenticate.

The username or password is wrong. Use the full email address as Username, check the mailbox password, and reset it in cPanel under Email › Email Accounts if needed.

SMTP Error: Could not connect to SMTP host.

The host name or port is wrong, or the connection is blocked. Check that Host is mail. followed by your domain and that Port is 587 with ENCRYPTION_STARTTLS. As an alternative, try port 465 with PHPMailer::ENCRYPTION_SMTPS. To see where the connection stops, remove the // before the SMTPDebug line and run the test again.

Peer certificate did not match expected CN

The SSL certificate on the mail server does not cover mail.example.com. Use your server host name as Host instead; see What is my server host name and IP address? to find it.

Failed opening required 'PHPMailer/src/PHPMailer.php'

PHP cannot find the PHPMailer files. Make sure the PHPMailer folder is in the same folder as test.php, and that the clone in the step above finished without errors.

Frequently asked questions

Can I install PHPMailer without SSH?

Yes. Download the ZIP file of PHPMailer from its GitHub page, upload it with cPanel File Manager, extract it and rename the folder to PHPMailer. Then create test.php next to it in File Manager.

Is there a limit on how many emails I can send with PHPMailer?

Yes. Messages sent through SMTP count toward your account's sending limit of (depending on the package, from 150 messages per hour). For newsletters to large lists, use a dedicated mailing service.

Related articles