PHP mail() returns true but the email never arrives
Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missingShort 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.
Fix it fast
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.log2Match what you see, then jump to its fix
- 1
The warning says sendmail_from is not set or a From header is missing.No From headerFix - 2
mail() 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
The server is Windows, and mail() fails or times out.On Windows no mail server listens at the SMTP settingFix - 4
The message arrives without headers, with headers inside the body, or is rejected.Header lines are separated the wrong wayFix
- 1
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/ | head2Fix
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.logConfirmed by php.net
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/sendmail2Fix
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
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.com3Verify
Restart the web server and send a test message.
php -r "var_dump(mail('you@example.org', 'test', 'body'));"Confirmed by php.net
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
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
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 fixCodeIgniter email does not send and shows no error message
$email->printDebugger() prints an empty result
CodeIgniter signals a failed email only by send() returning false, with no exception.
4 causes, each with a fixDovecot refuses to start: Can't load SSL certificate, no valid PEM certificate
doveconf: Error: ssl enabled, but ssl_cert not set
Dovecot reads the ssl_cert setting as the file's contents, not a path, so in 2.3 the value must start with a less-than sign.
3 causes, each with a fixLaravel tries to send mail to mailpit:1025 instead of your real SMTP server
Symfony\Component\Mailer\Exception\TransportException
Laravel is still using the development mail host from the starter .env, mailpit on port 1025, which only exists inside Laravel Sail.
3 causes, each with a fixLaravel mail fails because an address does not comply with RFC 2822
Email "Jane Doe <jane@example.com>" does not comply with addr-spec of RFC 2822.
Symfony Mailer checks every address when the Address object is created.
3 causes, each with a fixLaravel says the email was sent, but nothing arrives
Mail::to($user)->send() returns normally
Mail::send returns without an error when the message was accepted by the configured mailer, not when it was delivered.
3 causes, each with a fix