Content · Programming · PHP

PHP mail() Function: Usage and Examples

Submits an email message through PHP's configured mail.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Submits an email message through PHP's configured mail transport. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.

What It Does


Submits an email message through PHP's configured mail transport.

Syntax and Parameters


The examples below show a real call to mail with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.

Requirements: configured PHP mail transport; delivery cannot be inferred from a true result.

Example 1: Core Behavior


mail() reports transport submission, not delivery. In production prefer authenticated SMTP with queueing and verification.

<?php
$to = '[email protected]';
$subject = 'Account notice';
$body = 'Your report is ready.';
$headers = ['From' => '[email protected]', 'Content-Type' => 'text/plain; charset=UTF-8'];
$submitted = mail($to, $subject, $body, $headers);
echo $submitted ? 'Submitted to transport' : 'Mail transport failure';

Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.

Example 2: Alternative or Failure Case


mail() reports transport submission, not delivery. In production prefer authenticated SMTP with queueing and verification.

<?php
$to = '[email protected]';
$subject = 'Monthly statement';
$body = 'Your statement is available.';
$headers = ['From' => '[email protected]', 'Content-Type' => 'text/plain; charset=UTF-8'];
$submitted = mail($to, $subject, $body, $headers);
echo $submitted ? 'Submitted to transport' : 'Mail transport failure';

Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.

Practical Example: Applying the Rule


mail() reports transport submission, not delivery. In production prefer authenticated SMTP with queueing and verification.

<?php
$to = '[email protected]';
$subject = 'Support update';
$body = 'Your support request was updated.';
$headers = ['From' => '[email protected]', 'Content-Type' => 'text/plain; charset=UTF-8'];
$submitted = mail($to, $subject, $body, $headers);
echo $submitted ? 'Submitted to transport' : 'Mail transport failure';

In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.

Important Behavior and Edge Cases


Successful submission does not prove delivery; header injection and address validation must be prevented.

Comparison with Related Functions or Patterns


ApproachWhen it differs
mailSubmits an email message through PHP's configured mail transport.
Related API or patternAn SMTP client library offers authenticated transport, retries and richer diagnostics.

Common Beginner Mistakes


MistakeCorrect approach
Interpreting mail() returning true as confirmed recipient deliverySuccessful submission does not prove delivery; header injection and address validation must be prevented.
Substituting a related API without checking its semanticsAn SMTP client library offers authenticated transport, retries and richer diagnostics.

Key Takeaways


  • Submits an email message through PHP's configured mail transport.
  • Successful submission does not prove delivery; header injection and address validation must be prevented.
  • An SMTP client library offers authenticated transport, retries and richer diagnostics.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.