PHP mail() Function – Sending Email

This article explains PHP's mail() function, detailing its parameters, usage cautions, return values, and provides a complete code example for sending an email via SMTP, including formatting of recipient addresses, subject line restrictions, message body limits, and header construction.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP mail() Function – Sending Email

PHP's mail() function sends an email.

Parameters

to – Recipient email address or list. Must follow RFC 2822 format, e.g., [email protected], [email protected], [email protected], User <[email protected]>, or

User <[email protected]>, Another User <[email protected]>

.

subject – Email subject. This field must not contain any line‑break characters.

message – Message body. Lines must be separated by a LF (\n) and each line may not exceed 70 characters.

Caution – On Windows, if a line begins with a single period, it may be stripped; replace a single period with two periods to avoid this issue.

Return value – Returns TRUE if the mail was successfully accepted for delivery, otherwise FALSE.

Example

<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "
" .
    'Reply-To: [email protected]' . "
" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPEmailSMTPMail
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.