Nov 27, 2025
Matleena S.
4min Read
Nov 27, 2025
Matleena S.
4min Read
The PHP mail() function is a built-in way to send emails directly from a PHP script.
Developers often use it for simple tasks like sending contact form messages or testing email functionality.
While it is easy to use, the mail() function has limitations that make it better suited for learning and small projects than for production environments.
The PHP mail() function lets your website send emails without needing an external library. It works by calling the web server’s mail transfer agent (MTA) to deliver messages.;
The function takes in at least three parameters: the recipient’s email, the subject line, and the message body. You can also include optional headers like From, Reply-To, and content type.
This makes mail() one of the quickest ways to test sending emails in PHP. However, it doesn’t offer modern features like authentication, attachments, or reliable spam protection.
While mail() is simple, it lacks flexibility. PHPMailer, on the other hand, is a popular library that provides advanced email features like SMTP support, authentication, attachments, and HTML formatting.
Pros of mail():
Cons of mail():
If you need advanced features, consider using PHPMailer. We’ve covered it in detail in our guide on how to send emails using PHPMailer.
Here’s the basic syntax of the mail() function:
mail(to, subject, message, headers, parameters);
To send an email successfully, you need to set up correct headers:
<?php
$to = "example@domain.com";
$subject = "Test email from PHP";
$message = "This is a plain text email example.";
$headers = "From: hello@domain.com" . "rn" .
"Reply-To: hello@domain.com" . "rn" .
"X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers);
?>To test mail(), create a testmail.php file in your server root:
<?php
$to = "youraddress@example.com";
$subject = "PHP Mail Test";
$message = "This is a test message sent from PHP.";
$headers = "From: webmaster@yourdomain.com";
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully.";
} else {
echo "Email delivery failed.";
}
?>Upload this file and access it in your browser. If configured correctly, you should receive an email.
By default, mail() sends plain-text emails. To send HTML emails, add a Content-Type header:
<?php $to = "example@domain.com"; $subject = "HTML Email Example"; $message = " <html> <head> <title>HTML Email</title> </head> <body> <h1>Hello!</h1> <p>This is an <strong>HTML</strong> email sent with PHP.</p> </body> </html> "; $headers = "MIME-Version: 1.0" . "rn"; $headers .= "Content-type:text/html;charset=UTF-8" . "rn"; $headers .= "From: hello@domain.com" . "rn"; mail($to, $subject, $message, $headers); ?>
This allows you to format text, add headings, and style your message with HTML.
Validating email addresses helps prevent errors and reduce spam issues. PHP provides filter_var() for this:
<?php
$email = "user@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Valid email address.";
} else {
echo "Invalid email address.";
}
?>This ensures you only send emails to properly formatted addresses.
The mail() function can fail for several reasons. Here are the most common issues:
Emails often end up in spam if headers are incomplete or if the content looks suspicious. To reduce the risk:
Example of adding From and Reply-To headers:
$headers = "From: no-reply@yourdomain.comrn"; $headers .= "Reply-To: support@yourdomain.comrn"; $headers .= "X-Mailer: PHP/" . phpversion();
This error indicates a server configuration problem. Possible causes include:
Example of checking mail function in php.ini
; Make sure this is not disabled disable_functions =
Gmail and other providers require domain authentication to trust your messages. To fix this, ensure you have the following TXT entries in your DNS records:
Example of an SPF record:
v=spf1 include:_spf.[domain.tld] ~all
Example of a DKIM record:
Name: [selector]._domainkey.[domain.tld] Content: v=DKIM1; p=[series of numbers]
Example of a DMARC record:
Name: _dmarc.[domain.tld] Content: v=DMARC1; p=none; rua=mailto:[yourname]@[domain.tld]
This happens when the sender email does not belong to your domain. To resolve it:
Example of a correct sender address:
$headers = "From: no-reply@yourdomain.comrn";
Sometimes the mail() function does not throw an error but messages never arrive. Common reasons include:
Example of logging mail() results in PHP
if (mail($to, $subject, $message, $headers)) {
error_log("Mail sent successfully to $to");
} else {
error_log("Mail failed to send to $to");
}Improper headers cause delivery failures or corrupted emails. Follow these practices:
Example of correct HTML headers:
$headers = "MIME-Version: 1.0rn"; $headers .= "Content-type: text/html; charset=UTF-8rn";
The PHP mail() function is useful for learning, debugging, and sending simple test messages. However, it is not the best choice for production use. If you need reliable delivery, attachments, or SMTP authentication, move to a more powerful library like PHPMailer or connect to an external email service.
All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.