What is PHPMailer and what is it used for?

---
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}";
}
  1. 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!

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

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

And the last section to fill in with your data:

// 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>';  
  1. 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).

  2. 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:

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

Last updated