PHP Mail Reference
Preparing an email and delivering it are separate responsibilities. The course does not send messages or verify delivery to a real recipient.
How it works
mail depends on the host’s configured transport, and a successful handoff is not proof that a recipient received the message. A maintained mail library or service can supply MIME handling and a clearer delivery integration. Validate address/header inputs, prevent header injection and keep credentials outside source. Use an in-memory collecting adapter when testing a feature that should request a notification.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l ref-mail.php
php ref-mail.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Example
<?php
declare(strict_types=1);
$recipient = "reader@example.test";
$subject = "Loan receipt";
if (filter_var($recipient,FILTER_VALIDATE_EMAIL) === false || str_contains($subject,"\r") || str_contains($subject,"\n")) throw new InvalidArgumentException("Invalid mail fields");
$queue = [];
$queue[] = ["to"=>$recipient,"subject"=>$subject,"body"=>"Your fictional loan was recorded."];
echo count($queue), " message prepared; none sent\n";
Expected result
1 message prepared; none sentThe array is a test outbox, not a live transport. It checks that the application prepared one intended notification while making no external communication. Production email also needs user/privacy preferences, retry/idempotency decisions and appropriate provider configuration; these cannot be validated by a local format check.
Contracts to check
| API or term | Important boundary |
|---|---|
| Transport handoff depends on host configuration; no delivery guarantee. | |
| mb_send_mail | Requires mbstring and mail configuration; not a permission or delivery check. |
| filter_var with FILTER_VALIDATE_EMAIL | Format validation does not prove ownership or deliverability. |
| MIME headers and message bodies | Use a maintained implementation for encoding/attachments and prevent header injection. |
Related API names
This alphabetic inventory helps locate related calls. Availability was checked in the review runtime; it is not a recommendation to use every API or a claim that every overload/function was executed. Read the official entry for version, extension, failure and security details.
| Name | Review-runtime status |
|---|---|
ezmlm_hash() | Not available in this review runtime; check version and extension requirements |
mail() | Available in the PHP review runtime |
Check the official PHP manual for detailed version and API contracts.