Knowledge Base

Sending e-mail using PHPMailer with PHP & SMTP

This article shows several methods for installing and sending mail with PHP and PHPMailer.

You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:
  • Web hosting (Lite, Swift, or Turbo)
  • Reseller 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.

Install 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:
    [email protected] [~] cd public_html
  3. To install PHPMailer, type the following command:

    [email protected] [~/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.
    [email protected] [~/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.

    [email protected] [~/public_html] php mailer.php

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

Install 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:
    [email protected] [~] cd public_html
  3. To install PHPMailer, type the following command:

    [email protected] [~/public_html] git clone https://github.com/PHPMailer/PHPMailer.git
Testing the PHPMailer install

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.
    [email protected] [~/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].com';
    $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.

    [email protected] [~/public_html] php mailer.php

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

Windows PHPMailer install using the Git graphical interface

Windows hosting packages do not have a command line, so you must do the installation using the graphical tools in Plesk.

To install PHPMailer using Plesk, follow these steps:

  1. Log in to Plesk.
    If you do not know how to log in to your Plesk account, please see this article.
  2. Click the Git icon to open the Git tool:

    Plesk - Git icon

  3. Select Remote at the top of the screen:

  4. In the Remote Git Repository text box, paste the URL for the PHPMailer Git repository (https://github.com/PHPMailer/PHPMailer.git):

  5. Add a destination directory for the PHPMailer files:

    1. Click the destination link under Your Website.

    2. Click Create Folder.

    3. Type PHPMailer as the new folder name.
    4. Click the green checkmark to create the folder.
    5. Click Ok to set the new folder as the destination.
  6. Click OK to install PHPMailer.
Testing the PHPMailer install

To test the installation, follow these steps:

  1. Using the File Manager, create a new file named mailer.php.
  2. Click on the new file in file manager to open it for editing.
  3. Paste the following code into the file. Replace the values in red with the correct values for your account:
    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->Host = 'example.com';
    $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();
    ?>
  4. To save the file, click OK.
  5. Open the page in a web browser. The file does not produce any visible output, but an e-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.