PHP mail() doesn't work

PHP mail() doesn't work

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

The mail() Function Returns True But Emails Never Arrive

The core confusion in this scenario stems from a fundamental misunderstanding of what the PHP mail() function actually guarantees. When your script outputs "Email on the way," it is not confirming that an email was successfully delivered to the recipient's inbox, nor even that it left your local server. It only indicates that the local MTA (Mail Transfer Agent) accepted the message and handed it over for processing without throwing a PHP-level error.

In many shared hosting environments or local development setups using sendmail, the command simply writes the email to a spool file or passes it to the system's mail queue. The return value of true signifies that the function call itself was syntactically correct and the arguments were valid, not that the message reached xxx@example.com. This is why you see success messages despite the emails disappearing into the void.

The Role of Local MTAs

If you are running PHP on a dedicated server or a VPS where you have root access, the default mailer often relies on sendmail. To verify if your local system is actually queuing messages, inspect the spool directory:

ls -la /var/spool/mqueue/
# Or check the standard sendmail queue
mailq

If these directories contain files or output shows queued messages, the issue lies in the delivery phase, not the sending phase. The message is sitting in a queue waiting for an external SMTP server to pick it up and route it across the internet. If your server does not have direct MX records configured correctly or if the remote mail servers reject connections, those emails will sit there indefinitely until you clear the queue manually:

mailq | xargs -r /usr/sbin/sendmail -s /usr/sbin/sendmail -t -f <from_address> <<EOF > /dev/null
# Or simply flush the queue
find /var/spool/mqueue -type f -delete

However, most common hosting providers do not give you direct control over sendmail or the MTA in this manner. They abstract these details away to prevent users from causing conflicts with their shared infrastructure.

Shared Hosting Constraints

In a typical shared hosting environment (whether on Linux or Windows), PHP's mail() function does not send emails directly to an SMTP server like Gmail, Hotmail, or Yahoo. Instead, it invokes the host provider's internal mail server (often Postfix or Exim). That internal server then attempts to deliver the email.

If the host's internal server cannot connect to the recipient's domain (e.g., gmail.com), the email bounces silently or gets dropped. Crucially, because you are not sending from your own SMTP server, the receiving mail servers (Hotmail, Gmail) often flag these messages as suspicious. This is due to the "From" header mismatch and the lack of proper SPF, DKIM, and DMARC records associated with the shared hosting domain.

The headers in your code snippet set the From address to system@example.net. If example.net is not a verified sender identity on the hosting provider's side, major email providers will route these messages directly to spam or drop them entirely without notifying you. You cannot simply "turn off" your antivirus software or snitch tool on a shared host; you are bound by the provider's policies and the reputation of their IP ranges.

The Proper Solution: SMTP Libraries

Relying on mail() for critical functionality like registration activation is risky because it offers no feedback loop if delivery fails. If the email never arrives, your user remains unactivated, creating a broken workflow with no way to debug it via logs.

The industry standard solution is to use an SMTP library that connects directly to an external mail service using authenticated credentials. This bypasses the host's unreliable internal queue and establishes a direct connection to Gmail, Hotmail, or Yahoo's servers. The most robust approach in the PHP ecosystem is leveraging phpmailer or integrating with frameworks like Laravel.

If you are building on top of Laravel, consider utilizing the built-in mailer which supports sending via SMTP directly:

// In config/mail.php
return [
    'default' => env('MAIL_MAILER', 'smtp'),
    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'url' => env('MAIL_URL'),
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'local_domain' => env('MAIL_EHLO_DOMAIN'),
        ],
    ],
];

By switching to SMTP, you gain visibility into failures via bounce notifications and ensure that your emails are treated as legitimate traffic by recipient servers. For more complex needs, services like Mailgun or SendGrid provide APIs that handle the complexities of deliverability, ensuring your activation links actually reach the user's inbox rather than getting lost in a local spool file.

Our network