Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Email delivery

CodeIgniter email does not send and shows no error message

Error$email->send() returns false and nothing else happens

Short answer

CodeIgniter signals a failed email only by send() returning false, with no exception. Call send(false) so the debugger data is kept, print printDebugger(), then check the protocol, the SMTPPort and SMTPCrypto pair, and the 5-second SMTPTimeout.

  • Applies to CodeIgniter 4.5, CodeIgniter 4.6, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send without clearing the message data, then print the debugger. Without false, the data is cleared and there is nothing to print.

    if (! $email->send(false)) {
        echo $email->printDebugger(['headers']);
    }
  2. 2Match what you see, then jump to its fix

    1. 1printDebugger returns almost nothing, so you cannot see the server reply.send() cleared the data before printDebugger ranFix
    2. 2SMTP settings are in your config but the debugger shows no SMTP conversation.The protocol is still the default mail()Fix
    3. 3The attempt fails or times out right at the connection.SMTPCrypto does not match the portFix
    4. 4Failures appear only on some sends, mostly with a slow relay.SMTPTimeout of 5 seconds is too short for a slow serverFix

What does the error look like?

  • $email->send() returns false and nothing else happens
  • $email->printDebugger() prints an empty result

Cause 1 of 4

send() cleared the data before printDebugger ran

1Check

By default a successful or attempted send clears all email parameters. The documentation says to call send(false) first, or there is nothing to print.

grep -rn "printDebugger" app/

2Fix

Use send(false) while debugging and read the server messages the debugger returns.

$sent = $email->send(false);
if (! $sent) {
    log_message('error', $email->printDebugger(['headers', 'subject']));
}

3Verify

The log now contains the SMTP server messages for the failed attempt.

tail -n 40 writable/logs/log-$(date +%F).log

Confirmed by codeigniter.com

Did this fix it?

Cause 2 of 4

The protocol is still the default mail()

1Check

The protocol preference defaults to mail. SMTPHost, SMTPUser and the other SMTP values are only used when protocol is smtp.

grep -n "protocol\|SMTPHost" app/Config/Email.php

2Fix

Set the protocol to smtp in app/Config/Email.php, or pass it to initialize().

public string $protocol = 'smtp';
public string $SMTPHost = 'smtp.example.com';
public int $SMTPPort = 587;

3Verify

Send again and read the debugger. It now shows the SMTP server replies.

grep -n "protocol" app/Config/Email.php

Confirmed by codeigniter.com

Did this fix it?

Cause 3 of 4

SMTPCrypto does not match the port

1Check

SMTPPort defaults to 25. Port 465 uses TLS regardless of SMTPCrypto, and the documentation says port 465 should use an empty SMTPCrypto. Port 587 usually uses tls, which issues STARTTLS.

grep -n "SMTPPort\|SMTPCrypto" app/Config/Email.php

2Fix

Pair the port with the right crypto value.

// port 587
public int $SMTPPort = 587;
public string $SMTPCrypto = 'tls';

// or port 465
public int $SMTPPort = 465;
public string $SMTPCrypto = '';

3Verify

send(false) returns true.

grep -n "SMTPCrypto" app/Config/Email.php

Confirmed by codeigniter.com

Did this fix it?

Cause 4 of 4

SMTPTimeout of 5 seconds is too short for a slow server

1Check

SMTPTimeout defaults to 5 seconds.

grep -n "SMTPTimeout" app/Config/Email.php

2Fix

Raise the timeout a little. Do not set it so high that a web request hangs.

public int $SMTPTimeout = 15;

3Verify

Repeat the send that failed.

grep -n "SMTPTimeout" app/Config/Email.php

Confirmed by codeigniter.com

Did this fix it?

How do I stop it coming back?

  • Always test the return value of send() and log printDebugger() on failure.
  • Keep the port and SMTPCrypto pair in one config file, not scattered through controllers.
  • Send long mail jobs from a queue, not inside a web request.

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

PHP mail() returns true but the email never arrives

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

mail() returning true only means the message was accepted for delivery, not that it arrived.

4 causes, each with a fix

Email

PHPMailer addCC() adds no CC recipient

The mail arrives for the To address but not for the CC address

addCC() returns false when it rejects the address, and PHPMailer then sends without that CC.

3 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.