PHPMailer for PHP: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is PHPMailer and why is it needed?

\n\n

PHPMailer is one of the most popular and powerful libraries for sending emails in PHP. It provides a simple yet flexible interface for working with SMTP, POP3, and IMAP protocols. Unlike the standard PHP mail() function, PHPMailer offers many advantages:

\n\n
    \n
  • SMTP Authentication Support — secure sending through external mail servers (Gmail, Yandex, Mail.ru, your own SMTP server).
  • \n
  • HTML Email Support — create beautiful, formatted emails with images and styles.
  • \n
  • Attachments — ability to attach files of any type.
  • \n
  • UTF-8 Support — correct handling of Cyrillic and other languages.
  • \n
  • Header Injection Protection — security for your application.
  • \n
  • Support for Various Encodings — Base64, Quoted-Printable, and others.
  • \n
  • CC and BCC Support — blind and carbon copies.
  • \n
\n\n

The library is used in thousands of projects worldwide, including CMS like WordPress, Drupal, and Joomla. If you need to send emails from a PHP application — whether it's registration notifications, password resets, newsletters, or business proposals — PHPMailer will become your indispensable tool.

\n\n

Installing PHPMailer

\n\n

PHPMailer is installed via Composer, the dependency manager for PHP. If you don't have Composer yet, install it from the official website getcomposer.org.

\n\n

Open a terminal in your project's root folder and run the command:

\n\n
composer require phpmailer/phpmailer
\n\n

After successful installation, a vendor folder and a composer.json file will appear in your project. To include the library in your PHP script, use the Composer autoloader:

\n\n
require 'vendor/autoload.php';
\n\n

If you are not using Composer, you can download the library manually from GitHub and include it via require_once, but it is strongly recommended to use Composer for automatic dependency management.

\n\n

Quick Start with Code Example

\n\n

Let's send our first email using PHPMailer. In this example, we use the Gmail SMTP server. Make sure you have enabled access for less secure apps or use an App Password for two-factor authentication.

\n\n
<?php\n// Include the Composer autoloader\nrequire 'vendor/autoload.php';\n\n// Import PHPMailer classes\nuse PHPMailer\\PHPMailer\\PHPMailer;\nuse PHPMailer\\PHPMailer\\SMTP;\nuse PHPMailer\\PHPMailer\\Exception;\n\n// Create an instance of PHPMailer\n$mail = new PHPMailer(true);\n\ntry {\n    // Server settings\n    $mail->SMTPDebug = SMTP::DEBUG_OFF; // Disable debugging (for production)\n    $mail->isSMTP();                     // Use SMTP\n    $mail->Host       = 'smtp.gmail.com'; // SMTP server\n    $mail->SMTPAuth   = true;            // Enable authentication\n    $mail->Username   = 'your_email@gmail.com'; // Your email\n    $mail->Password   = 'your_password';        // Your password or app password\n    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL encryption\n    $mail->Port       = 465;             // Port for SSL\n\n    // Sender and recipient\n    $mail->setFrom('your_email@gmail.com', 'Your Name');\n    $mail->addAddress('recipient@example.com', 'Recipient Name');\n\n    // Email content\n    $mail->isHTML(true); // Set email format to HTML\n    $mail->Subject = 'Test email from PHPMailer';\n    $mail->Body    = '<h1>Hello!</h1><p>This is a test email sent using <b>PHPMailer<                    

Recommendations