PHPMailer is a widely used library for sending emails from PHP applications. It provides an easy-to-use interface for sending emails through various protocols including SMTP, Sendmail, and PHP's built-in mail function.
History
- PHPMailer was originally developed by Brent R. Matzelle in 2001. The initial versions were quite basic but provided essential functionality for email sending in PHP.
- In 2004, Marcus Bointon took over the maintenance of PHPMailer and has since been the primary maintainer. His involvement has led to significant enhancements, security fixes, and feature additions.
- The library saw a major refactoring with version 5.0 in 2012, which introduced namespaces, improved exception handling, and better compliance with modern PHP coding standards.
- Version 6.0, released in 2018, included further improvements like support for OAuth 2.0 authentication, and it moved to using the Composer package manager for easier dependency management.
Features
- Multiple Transport Methods: PHPMailer supports sending email via SMTP, Sendmail, qmail, and PHP's mail function, offering flexibility based on the server environment.
- SMTP Authentication: It supports SMTP authentication, including PLAIN, LOGIN, CRAM-MD5, and OAuth 2.0.
- HTML Emails: Users can send both plain text and HTML emails, with the ability to embed images and other resources directly into the email.
- Attachments: The library allows for the addition of multiple file attachments, with support for different file types.
- Security: It includes SSL/TLS support for secure SMTP connections, addressing common security concerns in email transmission.
- Debugging: PHPMailer provides detailed error reporting and debugging options to help developers diagnose and fix issues with email sending.
Usage
Here's a basic example of how PHPMailer is used:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true);
try {
//Server settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'yourusername@example.com';
$mail->Password = 'yourpassword';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('recipient@example.com', 'Recipient');
//Content
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body in bold!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
Resources
Related Topics