How to send e-mail using PHPMailer

This article demonstrates how to install PHPMailer and use it to send e-mails. With PHPMailer, you can send e-mail messages programmatically using PHP.

You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
  • Web hosting (Startup, Drive, Turbo Boost, or Turbo Max)
  • Reseller hosting
  • Managed WordPress hosting
For these hosting packages, you must use A2 Hosting servers. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.

Installing PHPMailer with Composer

Composer is a dependency manager for PHP that you can use to install packages required by a PHP project.

To install PHPMailer using Composer, follow these steps:

  1. Log in to your account using SSH.
  2. Change the directory to where you want to use the PHPMailer project. The location can be the document root of a domain as shown, or a subdirectory:
    user@server [~] cd public_html
  3. To install PHPMailer, type the following command:

    user@server [~/public_html] composer require phpmailer/phpmailer
    Composer suggests other packages to install, but they are not needed for this example.
Testing the PHPMailer installation

To test the installation, follow these steps:

  1. Create a PHP file to test the installation. You can create the file with the editor of your choice. In this procedure we use vi, but you can also use other command line editors or the graphical editor in your control panel. The file is named mailer.php in this example, but it can be any name ending in .php.
    user@server [~/public_html] vi mailer.php
  2. Type (or paste) the following code into the editor. Replace the items in red with the actual values for your account and save the file:

    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    require 'vendor/autoload.php';
    
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->Host = gethostname();
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
    $mail->setFrom('[email protected]');
    $mail->addAddress('[email protected]');
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the body.';
    $mail->send();
    ?>
  3. Test the setup by running the file from the command line or visiting the page in your web browser. The command line is shown below.

    user@server [~/public_html] php mailer.php

    The file does not produce any visible output, but a mail is sent to the recipient.

Installing PHPMailer with Git

Git is a version-control system that can be also be used to install software.

To install PHPMailer using Git, follow these steps:

  1. Log in to your account using SSH.
  2. Change the directory to where you want to use the PHPMailer project. The location can be the document root of a domain as shown, or a subdirectory:
    user@server [~] cd public_html
  3. To install PHPMailer, type the following command:

    user@server [~/public_html] git clone https://github.com/PHPMailer/PHPMailer.git
Testing the PHPMailer installation

To test the installation, follow these steps:

  1. Create a PHP file to test the installation. You can create the file with the editor of your choice. In this procedure we use vi, but you can also use other command line editors or the graphical editor in your control panel. The file is named mailer.php in this example, but it can be any name ending in .php.
    user@server [~/public_html] vi mailer.php
  2. Type (or paste) the following code into the editor. Replace the items in red with the actual values for your account and save the file:

    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->Host = gethostname();
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
    $mail->setFrom('[email protected]');
    $mail->addAddress('[email protected]');
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the body.';
    $mail->send();
    ?>
  3. Test the setup by running the file from the command line or visiting the page in your web browser. The command line is shown below.

    user@server [~/public_html] php mailer.php

    The file does not produce any visible output, but a mail is sent to the recipient.

More Information

PHPMailer supports many more features than are shown here. For more information about PHPMailer, please visit https://github.com/PHPMailer/PHPMailer.

Get PHPMailer Hosting

Article Details

  • Operating System: Linux Hosting
  • Level: Intermediate

Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.

We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.