PHPMailer Library: Professional Email Sending in PHP
Sending email is one of the key functions of any modern web application. The built-in PHP function mail() is often not flexible or secure enough: it does not support SMTP authentication, TLS/SSL encryption, sending HTML emails with attachments, and does not always work correctly on shared hosting. The solution to these problems is the PHPMailer library, which has become the de facto standard for sending email in PHP.
What is PHPMailer?
PHPMailer is a powerful and easy-to-use library for sending email from PHP scripts. It supports:
- Sending via an SMTP server (Gmail, Yandex, Mail.ru, corporate email);
- TLS and SSL encryption;
- HTML emails with attachments (images, PDFs, documents);
- UTF-8 encoding and working with any language (Russian, Chinese, etc.);
- Alternative plain-text for email clients without HTML;
- DKIM and SPF signing for spam protection;
- Working via local sendmail or the built-in mail() as a fallback.
Installing PHPMailer
The easiest way is via Composer. In the terminal, run the command in the root of your project:
composer require phpmailer/phpmailerIf Composer is not used, you can download the archive from GitHub and include the files manually:
require_once '/path/to/PHPMailer/src/PHPMailer.php';require_once '/path/to/PHPMailer/src/SMTP.php';require_once '/path/to/PHPMailer/src/Exception.php';Basic Example of Sending an Email via Gmail SMTP
Let's look at a minimal configuration for sending an email via the Gmail SMTP server. Note: you will need to create an app password in your Google account settings (or use the less secure "access to less secure apps" option).
<?phpuse PHPMailer\\PHPMailer\\PHPMailer;use PHPMailer\\PHPMailer\\SMTP;use PHPMailer\\PHPMailer\\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try { // Server settings $mail->SMTPDebug = SMTP::DEBUG_OFF; // Debugging (0 - off, 2 - verbose) $mail->isSMTP(); // Use SMTP $mail->Host = 'smtp.gmail.com'; // SMTP server $mail->SMTPAuth = true; // Enable authentication $mail->Username = 'your-email@gmail.com'; // SMTP username $mail->Password = 'your-app-password'; // SMTP password (app password) $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // SSL $mail->Port = 465; // Port for SSL (587 for TLS)
// Sender and recipient $mail->setFrom('from@example.com', 'Sender Name'); $mail->addAddress('to@example.com', 'Recipient Name'); // Can add multiple $mail->addReplyTo('reply@example.com', 'Reply To');
// Attachments (optional) $mail->addAttachment('/path/to/file.pdf', 'Name.pdf');
// Email content $mail->isHTML(true); // HTML format $mail->Subject = 'Hello from PHPMailer!'; $mail->Body = '<b>This is an HTML email</b> with Russian text.'; $mail->AltBody = 'This is alternative text for email clients without HTML.';
$mail->send(); echo 'Message sent successfully';} catch (Exception $e) { echo "Error: {$mail->ErrorInfo}";}?>Sending via Yandex.Mail
For Yandex.Mail, the parameters change slightly. Important: in your mailbox settings, you must allow access via the IMAP/SMTP protocol and create an app password (if two-factor authentication is enabled).
$mail Also in library
Working with Images in Rust: The image Library and Beyond
Working with ZIP Archives in Go: A Complete Guide to the archive/zip Package