Sending Email in PHP: From the Built-in mail() to Powerful Libraries
Sending emails is one of the key functions of any web application: user registration, password recovery, notifications, newsletters. In PHP, there are several approaches: from the simplest built-in mail() function to specialized libraries such as PHPMailer and SwiftMailer. In this article, we will thoroughly examine each method, show code examples, and provide recommendations for choosing.
1. The Built-in mail() Function
The mail() function is the simplest way to send an email. It uses the mail server configured in the PHP settings (sendmail_path or SMTP). However, its capabilities are limited, and it does not support modern requirements: HTML formatting, attachments, SMTP authentication.
Basic Example
<?php$to = 'user@example.com';$subject = 'Email Subject';$message = 'Hello! This is a test email.';$headers = 'From: no-reply@mysite.ru' . "\\r" . 'Reply-To: support@mysite.ru' . "\\r" . 'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) { echo 'Email sent';} else { echo 'Sending error';}?>Disadvantages of mail()
- No support for HTML emails (you need to manually specify
Content-Type: text/html) - Inability to attach files without complex MIME manipulations
- No encryption or SMTP authentication
- Emails often end up in spam due to missing proper headers
- Difficult to debug errors
For modern projects, mail() is not recommended. It is better to use specialized libraries.
2. PHPMailer — The Gold Standard
PHPMailer is the most popular library for sending email in PHP. It supports SMTP, SSL/TLS, HTML emails, attachments, alternative text, multiple recipients, and much more. It is installed via Composer:
composer require phpmailer/phpmailerExample of Sending via SMTP (Gmail)
<?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_SERVER; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'your@gmail.com'; $mail->Password = 'your-app-password'; // app password $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->Port = 465;
// Sender and recipients $mail->setFrom('from@example.com', 'Sender'); $mail->addAddress('user@example.com', 'Recipient'); $mail->addReplyTo('info@example.com', 'Information');
// Attachments (optional) $mail->addAttachment('/path/to/file.pdf', 'document.pdf');
// Content $mail->isHTML(true); $mail->Subject = 'Email Subject'; $mail->Body = '<h1>Hello!</h1><p>This email contains <b>HTML</b>.</p>'; $mail->AltBody = 'Hello! This is the text version of the email.';
$mail->send(); echo 'Email sent';} catch (Exception $e) { echo "Error: {$mail->ErrorInfo}";}?>Advantages of PHPMailer
- Full SMTP support with authentication
- Automatic generation of MIME headers
- Support for attachments and embedded images
- Works with HTML and plain text versions of the email
- Detailed error messages
- Active community support
3. SwiftMailer — An Alternative with a Flexible Architecture
SwiftMailer is another powerful library (now replaced by
Also in library
Elasticsearch client for Go (Golang): complete guide to working with ES
Working with Kafka in C# (.NET): Confluent.Kafka Library
Working with Apache Kafka in C++: Libraries and Examples
CLI frameworks for TypeScript: complete overview and usage examples
Working with ZIP Archives in PHP: A Complete Guide to ZipArchive