> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Sending e-mail using PHPMailer

> Send emails with PHP using PHPMailer. Learn installation with Composer and Git, plus get code samples to start quickly.

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.

<Warning>
  **Important**

  You cannot use external SMTP servers to send e-mail messages if you have one of the following hosting packages:

  * Shared web hosting

  * Reseller hosting

  * Managed WordPress hosting

  For these hosting packages, you must use hosting.com servers. Other hosting packages have fewer restrictions, and can use some external SMTP servers to send e-mail messages.
</Warning>

## 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:

   ```bash theme={null}
   user@server [~] cd public_html
   ```

3. To install PHPMailer, type the following command:

   ```bash theme={null}
   user@server [~/public_html] composer require phpmailer/phpmailer
   ```

   > 📘 Note
   >
   > 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*.

   ```bash theme={null}
   user@server [~/public_html] vi mailer.php
   ```

2. Type (or paste) the following code into the editor. Replace the email addresses and password 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 = 'sender@example.com';
   $mail->Password = 'password';
   $mail->setFrom('sender@example.com');
   $mail->addAddress('recipient@example.com');
   $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.

   ```bash theme={null}
   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:

   ```bash theme={null}
   user@server [~] cd public_html
   ```

3. To install PHPMailer, type the following command:

   ```bash theme={null}
   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*.

   ```bash theme={null}
   user@server [~/public_html] vi mailer.php
   ```

2. Type (or paste) the following code into the editor. Replace the email addresses and password 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 = 'sender@example.com';
   $mail->Password = 'password';
   $mail->setFrom('sender@example.com');
   $mail->addAddress('recipient@example.com');
   $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.

   ```bash theme={null}
   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](https://github.com/PHPMailer/PHPMailer).

## Related articles

* [Using PHP to send e-mail messages](/docs/using-php-to-send-e-mail-messages)

* [Using Python to send e-mail messages](/docs/using-python-to-send-e-mail-messages)

* [Using Git with Plesk](/docs/using-git-with-plesk)
