Welcome Guest, Not a member yet? Register   Sign In
$email->send() always returns false but the email is received

Hey there,

so the problem is pretty much in the title, the $email->send() function always returns false, even though I receive the email without any problem on every email address (to, cc, bcc).
Code sample below. I'm using the latest branch of CI4.

$email = Services::email();
$config = new Email();

foreach($files as $file)

$email->setFrom($config->fromEmail, $config->fromName);
$email->setHeader('MyCustomHeader', $myCustomHeaderValue);

if (!$email->send()) {
   throw new \Exception("Email sending failed");

Any idea? Thanks in advance!

To dig into your question please tell what is the he mail sending protocol (mail, sendmail, smtp) you are using?
Also if you are in Windows or Linux environment, virtual host or private server, what is PHP version, etc.

I am using email sending in CI4 in my project pretty much same way you do and it works fine (CentOS7, PHP 7.4, CI4.0.2, smtp).

My mail sending protocol is SMTP. I'm on Linux (x86_x64) and my PHP version is 7.3, with the latest CI4 stable branch.

Anyway, the problem seems to be "fixed" now, even though absolutely nothing has changed.
The PC was running all night, so there was no restart that could have fix something, nor the code was modified in any way.
Right now, most of the time everything goes okay, but sometimes it still returns false, even though the email went through, so it's not too reliable. Could be something on my end? If so, how can I debug it properly to know what's going on?

Thanks for the answers!

(This post was last modified: 03-11-2020, 01:06 PM by zahhar.)

Thanks, this makes sense. Seems we have everything to troubleshoot your problem.

Email is sent through SMTP using sendWithSMTP() function declared in system/Email/Email.php class: 

Looks that this function can return false even if email was sent successfully! Take a look at this code (comments are mine):

PHP Code:
        $this->sendData($this->headerStr preg_replace('/^\./m''..$1'$this->finalBody));
$this->sendData($this->newline '.'); //finished sending body
$reply $this->getSMTPData(); //reads reply from SMTP server
$this->setErrorMessage($reply); //no idea what happens
$this->SMTPEnd(); //closes connection
if (strpos($reply'250') !== 0//reply should start with 250 only
$this->setErrorMessage(lang('Email.SMTPError', [$reply]));
false//this line returns FALSE even if email was sent succesfully

So in your case email is sent to SMTP server, but server sometimes responds with some other code then only expected code 250. Here is the full list of codes, however, particular SMTP server software may implement additional codes, or have other meaning:

I would suggest you to debug sendWithSMTP() function by writing $reply variable values you are getting from SMTP server to log file, and then to analyze if there are any other responses then 250 (my guess will be 252 or 251, both are good candidates based on description).

If you could confirm and provide more information, we could improve this part in CI4. 

Looking forward to hearing from you.

Theme © iAndrew 2016 - Forum software by © MyBB