A complete set of working PHPMailer examples, one per sending transport and message type. Every snippet below is self-contained: install PHPMailer, paste, change the addresses and credentials, run. All examples target PHPMailer 6.x. These pages replace the example scripts that were distributed with the original PHPMailer package (test_mail_basic.php, test_smtp_advanced.php and the rest). The library itself is maintained at github.com/PHPMailer/PHPMailer, where the current example folder also lives. Before you start composer require phpmailer/phpmailer Every example assumes these four lines at the top of the file: <?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerSMTP; use PHPMailerPHPMailerException; require ‘vendor/autoload.php’; Passing true to the constructor turns on…
Author: sunshyne
Once more than one script in a codebase sends email, the same fifteen lines of SMTP configuration start appearing in several places, and drift apart. Subclassing PHPMailer puts that configuration in one file, so a changed provider is a one-line edit instead of a search across the project. Examples target PHPMailer 6.x. The basic pattern <?php namespace AppMail; use PHPMailerPHPMailerPHPMailer; class AppMailer extends PHPMailer { public function __construct(?bool $exceptions = true) { parent::__construct($exceptions); $this->isSMTP(); $this->Host = getenv(‘SMTP_HOST’); $this->Port = (int) getenv(‘SMTP_PORT’) ?: 587; $this->SMTPAuth = true; $this->Username = getenv(‘SMTP_USER’); $this->Password = getenv(‘SMTP_PASS’); $this->SMTPSecure = self::ENCRYPTION_STARTTLS; $this->CharSet = self::CHARSET_UTF8; $this->Timeout =…
Every public property of PHPMailer 6.x you are likely to set, with its type, default and what it actually changes. Properties are set directly on the instance. There are no setters for most of them: $mail->Host = ‘smtp.example.com’; $mail->Port = 587; $mail->CharSet = PHPMailer::CHARSET_UTF8; SMTP connection PropertyTypeDefaultPurpose $HoststringlocalhostSMTP server. Multiple hosts can be given semicolon-separated as a failover list. $Portint25Set to 587 for STARTTLS, 465 for implicit TLS. $SMTPSecurestring”ENCRYPTION_STARTTLS (tls) or ENCRYPTION_SMTPS (ssl). $SMTPAuthboolfalseEnable authentication. Needed for every hosted provider. $Usernamestring”Sometimes the full address, sometimes the local part. Providers differ. $Passwordstring”Read it from the environment, not from source. $AuthTypestring”Forces a…
A reference for the PHPMailer 6.x methods you will actually call, grouped by what they do, with signatures and the behaviour worth knowing. The complete API is in the source at github.com/PHPMailer/PHPMailer; this page covers what comes up in practice. Constructor public function __construct(?bool $exceptions = null) Pass true to make failures throw PHPMailerPHPMailerException instead of returning false. Do this. The default leaves you to check every return value by hand, and the day you forget is the day email stops working silently. $mail = new PHPMailer(true); Choosing the transport MethodEffect isSMTP(): voidSend over SMTP. Requires Host, and usually SMTPAuth,…
PHPMailer’s default transport is PHP’s built-in mail() function. It needs no configuration at all, which makes it the fastest way to get a message out. And the reason it keeps being used in places where it should not be. This page covers how to use it properly, and how to recognise when you have outgrown it. Examples target PHPMailer 6.x. For the transport you probably want instead, see the SMTP example. Minimal example <?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException; require ‘vendor/autoload.php’; $mail = new PHPMailer(true); try { $mail->setFrom(‘from@example.com’, ‘Your App’); $mail->addAddress(‘recipient@example.com’, ‘Recipient Name’); $mail->Subject = ‘Contact form submission’; $mail->Body = “Name:…
Sending through an authenticated SMTP server is the right default for any message that has to arrive. It gives you TLS, real authentication, and — unlike PHP’s mail() — an actual error when something goes wrong. This page covers the configuration in full, plus every failure mode worth knowing about. All examples target PHPMailer 6.x. For the other transports, see the examples collection. The complete example <?php use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerSMTP; use PHPMailerPHPMailerException; require ‘vendor/autoload.php’; $mail = new PHPMailer(true); try { // Transport $mail->isSMTP(); $mail->Host = ‘smtp.example.com’; $mail->SMTPAuth = true; $mail->Username = getenv(‘SMTP_USER’); $mail->Password = getenv(‘SMTP_PASS’); $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; $mail->Port…
How to install PHPMailer 6.x — with Composer, or by hand when Composer is not available — plus the PHP extensions it needs and how to confirm the installation works before you write any application code. Requirements RequirementNeeded for PHP 5.5+The library itself. Current PHP versions are fully supported. ext-opensslAny TLS connection — so any real SMTP server. ext-mbstringCorrect encoding of non-ASCII subjects and bodies. ext-filterAddress validation. Enabled by default in practice. ext-hashDKIM signing. Check what you have before installing: php -m | grep -E ‘openssl|mbstring|filter|hash’ php -r ‘echo PHP_VERSION, PHP_EOL;’ Missing openssl is the one that will stop you…
This tutorial takes you from an empty project to a reliable, authenticated email send in PHP using PHPMailer 6.x. The version currently maintained at github.com/PHPMailer/PHPMailer. Each step builds on the previous one, and the failure modes are covered as they come up rather than left to a troubleshooting appendix. If you only want a snippet to paste, the examples page has one per transport. 1. Why not just use mail()? PHP’s mail() function works, but it gives you almost nothing: no SMTP authentication, no TLS, no MIME handling, and no useful error reporting. It returns true the moment the local…
PHPMailer is the most widely used library for sending email from PHP. It has been in use since 2001, ships in WordPress, Drupal, Joomla and thousands of applications, and is installed millions of times a month through Composer. If a PHP application sends mail, there is a good chance PHPMailer is doing it. This domain hosted the original PHPMailer website and its documentation for over a decade. Development moved to GitHub years ago: github.com/PHPMailer/PHPMailer is the authoritative source for releases, issues and security advisories. The pages here are tutorials, worked examples and a reference for using the library day to…