# What is PHPMailer and what is it used for?

````md
---
description: 'Hashtags: #Mail() #Form'
---

# What is PHPMailer and what is it used for?

On some servers, the **mail()** function in PHP may not be available. \
It is increasingly common for this function to be disabled on servers as a security measure. \
It is usually disabled for protection against SPAM, Mail Spoofing, and Proxy Mail Servers. \
If any of the hosted websites are compromised by a malicious user, the mentioned risks are avoided by disabling this function.

### PHPMailer library

The PHPMailer library allows us, among other functionalities, to establish an SMTP connection with a mail server. This SMTP server will actually send our email.

The official project website on GitHub contains documentation with installation and configuration options. \
The link to GitHub is: [https://github.com/PHPMailer/PHPMailer](https://github.com/PHPMailer/PHPMailer).

### Installing PHPMailer

1. Download **PHPMailer** from [https://github.com/PHPMailer/PHPMailer](https://github.com/PHPMailer/PHPMailer). \
   Click on the green **Code** button and then click on **Download ZIP** in the **Clone** box.

<figure><img src="../../../.gitbook/assets/CorreoErroresPHPMailer01.png" alt=""><figcaption></figcaption></figure>

<figure><img src="../../../.gitbook/assets/CorreoErroresPHPMailer02.png" alt=""><figcaption></figcaption></figure>

2. Unzip the downloaded file "**PHPMailer-master.zip**" and rename the directory **PHPMailer-master** to **PHPMailer**.
3. Copy the **PHPMailer** directory into the root of your website (e.g., public\_html). To verify that the paths are correct, there should be, from the root of our project: **/PHPMailer/src/PHPMailer.php**

### Configuration Example

1. Save the following code in a file, for example, **example.php**

```php
// Some code<?php
// Display PHP errors (Disable in production)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Include PHPMailer library
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

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

// Start
$mail = new PHPMailer(true);

try {
    // SMTP Configuration
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                         // Display output (Disable in production)
    $mail->isSMTP();                                               // Enable SMTP sending
    $mail->Host  = 'SET_SMTP_SERVER';                              // SMTP server
    $mail->SMTPAuth  = true;                                       // SMTP identification
    $mail->Username  = 'SET_SMTP_USER';                            // SMTP user
    $mail->Password  = 'SET_SMTP_PASSWORD';                        // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port  = 587;
    $mail->setFrom('hello@example.com', 'Your name');              // Email sender

    // Recipients
    $mail->addAddress('test@example.com', 'Recipient name');       // Recipient email and name

    // Email content
    $mail->isHTML(true);
    $mail->Subject = 'Subject of the email';
    $mail->Body  = 'Email content <b>in HTML!</b>';
    $mail->AltBody = 'Email content in plain text for email clients that do not support HTML';
    $mail->send();
    echo 'The message has been sent';
} catch (Exception $e) {
    echo "The message was not sent. Mailer Error: {$mail->ErrorInfo}";
}
````

2. Modify the connection parameters in this file, the sender's and recipient's email where we will send our test email.\
   Look for the following lines in the script and replace the data with yours.\
   **It is very important to configure this data using a valid mailbox that you can access!**

```php
// SMTP Configuration
$mail->Host  = 'mail.yourdomain.com';   // Configure the SMTP server here
$mail->Username  = 'mailbox@yourdomain.com';  // This will be the mailbox that sends the emails
$mail->Password  = 'mailbox password';  // Your mailbox password
$mail->setFrom('mailbox@yourdomain.com', 'Your name'); // Configure your name and the sender mailbox
```

Below you will see this section to fill in with your data:

```php
// Recipients    
$mail->addAddress('test@anotherdomain.com', 'Recipient name');  // Recipient email and name
```

And the last section to fill in with your data:

```php
// Email content 
$mail->isHTML(true);
$mail->Subject = 'Subject of the email';  // The subject that the sent email will show
$mail->Body  = 'Email content <b>in HTML!</b>';  
```

3. Upload via **FTP** or using **cPanel's File Manager**, the **PHPMailer** directory and the **example.php** file to the root of your website's public directory (public\_html).
4. Run the example from `https://yourdomain.com/example.php`.\
   It will display the debug of the process and then you will receive the email:\
   `"The message has been sent"`\
   \
   If it shows any error, whether PHP or SMTP connection, you will need to debug the source of the problem. The error message you would receive in this case is:\
   `"The message was not sent. Mailer Error: <error type>"`

In production, remember to disable PHP and SMTP error debugging by changing the first lines of the test code:

```php
ini_set('display_errors', 1);    // Change 1 to 0
ini_set('display_startup_errors', 1); // Change 1 to 0
error_reporting(E_ALL); // Change "E_ALL" to 0
```

```
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.sitioshispanos.com/menu-principal/guias/correo-electronico/problemas-con-sus-correos/phpmailer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
