phpmailer/phpmailer
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
PHPMailer is a highly popular, full-featured email creation and transfer class for PHP. Used by a long list of open-source projects, PHPMailer provides integrated SMTP support, the ability to send emails with multiple To, CC, BCC, and Reply-to addresses, Multipart/Alternative emails, support for UTF-8 content, automatic validation of email addresses, protection against header injection attacks, DKIM and S/MIME signing support, and much more. Its comprehensive feature set makes it a preferred choice among PHP developers for sending emails from their code.
To use PHPMailer, you first need to install it. The recommended way to install PHPMailer is via Composer. Here is a code snippet showing how to do this:
composer require phpmailer/phpmailer
Once installed, you can use PHPMailer to send emails. Here's a basic example:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
In this example, an SMTP server is used to send the email. To, From, Subject, and Body fields of the email are set using the relevant PHPMailer methods.
You can find extensive documentation for PHPMailer on its GitHub wiki. It includes a wide array of information, from basic usage to troubleshooting guides, API documentation, and examples. Detailed API-level documentation can be generated by running phpdoc
in the top-level folder.
In addition, you'll find many examples in the examples folder on the GitHub repository. Before you ask a question about "SMTP Error: Could not connect to SMTP host.", make sure to check out the troubleshooting guide as it's frequently updated. The GitHub wiki is the best place to start if you want to learn more about PHPMailer.