PHP mail function is not working on my server?
When a mail() call returns true but the recipient never receives the message, the issue almost always lies in server-side configuration rather than your application logic. Your code snippet indicates that the function executed without throwing a PHP exception; however, many shared hosting environments silently discard messages if they fail specific security checks or lack valid delivery paths.
The Silent Failure of mail()
The most critical flaw in your current implementation is how you are constructing the $headers array. You are setting the From address to the value provided by the user ($_POST['email']). While this might work on a local machine or a dedicated server with relaxed security policies, it frequently triggers spam filters or bounce mechanisms on shared hosting providers.
Shared hosts often have strict rules preventing you from spoofing arbitrary addresses in the From header. If your host detects that the sender address does not match a verified domain associated with your account, they may reject the message at the SMTP level before it leaves their network. Consequently, your PHP script receives a success signal because the local queue accepted the data, but the remote server rejected it later, resulting in no email arriving in the inbox.
To resolve this, you must verify if your hosting provider allows custom From addresses. If not, change the logic to use a generic address provided by the host, such as noreply@yourdomain.com. You can often find the allowed "return-path" or "sender" address in your hosting control panel under email settings. Update your code accordingly:
$headers = "From: noreply@example.com \r\n"; // Use a verified domain
$headers .= "Reply-To: $email \r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
Checking Server Logs for Real Errors
Relying solely on the return value of mail() is a common pitfall. The function returns true if the message was handed off to the local MTA (Mail Transfer Agent), not if it reached the destination. To debug further, you need to inspect the server error logs.
On Linux-based servers, these are typically located in /var/log/mail.log, /var/log/syslog, or /var/log/maillog. Look for entries corresponding to your request timestamp. You will likely see messages indicating a rejection reason, such as "550 5.7.1 Relaying denied" or "554 X-Spam-Status: Yes".
If you cannot access the logs directly, check your hosting dashboard's error log section. Many providers offer a specific "Email Delivery Status" tool in their control panel that shows bounce reasons for queued messages.
Implementing Robust Error Handling
Your current code uses error_get_last() inside an else block triggered by mail(). This approach is ineffective because mail() does not throw exceptions on failure, nor does it trigger PHP error logs for SMTP rejections in a way that error_get_last() can capture. The function simply returns false only if the local queue rejects the message immediately (e.g., missing From header).
A better strategy is to implement custom logging that records the headers and subject before sending, regardless of the return value. This allows you to audit exactly what was sent when an email fails to arrive. You should also consider switching to a transactional email service like SendGrid, Mailgun, or AWS SES if your current provider's SMTP limitations persist. These services handle deliverability, bounce management, and spam filtering far more effectively than the native PHP mail() function.
If you are building this on top of a Laravel application, utilize the built-in Mail facade which abstracts these complexities. You can configure your .env file to use an SMTP driver that automatically handles credentials and connection timeouts:
'_MAIL_DRIVER' => 'smtp',
'MAIL_HOST' => 'smtp.mailgun.org',
'MAIL_PORT' => 587,
'MAIL_USERNAME' => 'your_username',
'MAIL_PASSWORD' => 'your_password',
Final Recommendation
Before switching infrastructure, ensure your From address is legitimate and verified with your hosting provider. If the server continues to drop emails despite correct headers, it indicates a hard limit on your account's email quota or domain reputation. At that point, migrating to an external transactional email provider is the standard industry solution for reliable communication.