Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

PHP mail() returns true but the email never arrives

ErrorWarning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing

Short answer

mail() returning true only means the message was accepted for delivery, not that it arrived. Turn on mail.log to see each call, then check the From header, the sendmail_path program (or SMTP setting on Windows), and header line breaks. Delivery after that is the mail server's job.

  • Applies to PHP 8.3, PHP 8.4, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log every mail() call, then send one test message and read the log.

    ; php.ini (INI_SYSTEM or per-directory)
    mail.log = /var/log/php-mail.log
  2. 2Match what you see, then jump to its fix

    1. 1The warning says sendmail_from is not set or a From header is missing.No From headerFix
    2. 2mail() returns false, or true, and the mail log shows the call but the server mail log shows nothing.The sendmail program is missing or misconfiguredFix
    3. 3The server is Windows, and mail() fails or times out.On Windows no mail server listens at the SMTP settingFix
    4. 4The message arrives without headers, with headers inside the body, or is rejected.Header lines are separated the wrong wayFix

What does the error look like?

  • Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing

Cause 1 of 4

No From header

1Check

The manual says a message must contain a From header, set with the additional_headers parameter or a default in php.ini.

grep -rn "mail(" src/ app/ public/ | head

2Fix

Pass a From header, with an address on a domain you control.

<?php
$headers = 'From: webmaster@example.com' . "\r\n" .
           'Reply-To: webmaster@example.com';
mail('you@example.org', 'Subject', "Line 1\r\nLine 2", $headers);

3Verify

Send one message and look for the call in the mail log.

tail -n 5 /var/log/php-mail.log

Confirmed by php.net

Did this fix it?

Cause 2 of 4

The sendmail program is missing or misconfigured

1Check

On Linux, mail() hands the message to the program in sendmail_path (default /usr/sbin/sendmail -t -i). If no mail transfer agent provides that program, nothing is sent.

php -r "echo ini_get('sendmail_path'), PHP_EOL;"
ls -l /usr/sbin/sendmail

2Fix

Install and configure a mail transfer agent that provides the program, or point sendmail_path at a working one. Keep the -t -i options.

; php.ini
sendmail_path = "/usr/sbin/sendmail -t -i"

3Verify

Restart PHP-FPM or the web server, send a test message and read the MTA's own log.

php -r "var_dump(mail('you@example.org', 'test', 'body', 'From: webmaster@example.com'));"

Confirmed by php.net

Did this fix it?

Cause 3 of 4

On Windows no mail server listens at the SMTP setting

1Check

On Windows, mail() does not use a local binary; it opens a socket to the host in SMTP and the port in smtp_port (defaults localhost and 25). A server must be listening there.

php -r "echo ini_get('SMTP'), ':', ini_get('smtp_port'), PHP_EOL;"

2Fix

Set SMTP, smtp_port and sendmail_from to a mail server that accepts your connection.

; php.ini
SMTP = mail.example.com
smtp_port = 25
sendmail_from = webmaster@example.com

3Verify

Restart the web server and send a test message.

php -r "var_dump(mail('you@example.org', 'test', 'body'));"

Confirmed by php.net

Did this fix it?

Cause 4 of 4

Header lines are separated the wrong way

1Check

The manual says lines are separated with CRLF (\r\n) and that multiple extra headers are separated with CRLF. Check the strings you pass.

grep -rn "additional_headers\|From:" src/

2Fix

Join headers with \r\n, or pass them as an array.

<?php
mail('you@example.org', 'Subject', 'Body', [
    'From' => 'webmaster@example.com',
    'Reply-To' => 'webmaster@example.com',
]);

3Verify

Send a test and view the raw message in the receiving mailbox.

php -r "var_dump(mail('you@example.org', 't', 'b', ['From' => 'webmaster@example.com']));"

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • For anything important, send through an SMTP library and an authenticated provider, and log the provider's response.
  • Never put unchecked user input into mail headers; strip \r and \n first.
  • Keep mail.log on in staging so every call is visible.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Email

Mail from your own domain keeps landing in the spam folder

dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=example.com

Open a message that went to spam and read its Authentication-Results header: it shows whether SPF, DKIM and DMARC passed, and for which domain.

5 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.