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('[email protected]', 'Your name'); // Email sender
// Recipients
$mail->addAddress('[email protected]', '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}";
}PreviousWhat do the error codes mean when I try to send emails?NextWhy is the mail() function disabled?
Last updated
Was this helpful?