PHP (PHPMailer)

PHPMailer against a sandbox, without a framework in the way.

Before you start

  • PHP 7.4 or later
  • phpmailer/phpmailer

1Install PHPMailer

composer require phpmailer/phpmailer

2Send a test message

Your sandbox credentials
<?php

use PHPMailer\PHPMailer\PHPMailer;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.mailclutch.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'mc_0000000000000000';
$mail->Password = 'pw_0000000000000000';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

$mail->setFrom('staging@example.com');
$mail->addAddress('anyone@example.com');
$mail->Subject = 'Sandbox check';
$mail->isHTML(true);
$mail->Body = '<h1>Hello from PHP</h1>';

$mail->send();

Watch it land

Open the sandbox and the message is there — rendered HTML, plain text, raw source, attachments, and the SPF, DKIM and DMARC verdicts. It updates live, so you can leave it open next to your terminal.

Nothing is ever delivered onward, whatever address you send to; claim the inbox with an account and it stays.

Awaiting your email

Send from any address; it stops here rather than going onward. The inbox keeps what it catches for 24 hours.

What usually goes wrong

  • mail() cannot be pointed here

    PHP's mail() hands the message to a local binary, which has no idea about SMTP credentials. Either send through PHPMailer or Symfony Mailer, or relay the local MTA — see the Postfix guide.

  • Debugging a refused connection

    $mail->SMTPDebug = 2; prints the whole conversation, which usually names the problem on the first line.

Official documentationPHPMailer (opens in a new tab)