Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Replace error handler with error_get_last() function #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

flancer64
Copy link

Hi, guys.

I just found that mail() function does not generate the proper error that can be handled by handleMailErrors() method ("Temporary error handler for PHP native mail()" as pointed in the phpdoc). I have an msmtp as email agent with following settings in php.ini:

sendmail_path = "/usr/bin/msmtp -C /etc/.msmtp.fpm --logfile /var/log/msmtp.log --read-envelope-from -a gmail -t"

I have an error in \Zend\Mail\Transport\Sendmail::mailHandler if

$parameters = " -f'[email protected]'";

in

$result = mail($to, $subject, $message, $headers, $parameters);

As I see, there is a conflict between --read-envelope-from option of msmtp and -f'[email protected]' parameter of mail function. So, mail function returns false in this case.

But this code does not handle the error as expected. The breakpoint in handleMailErrors method does not activated when error occurs (I have PHP 7.2.19 & PHPStorm 2019.1.3).

I found that error_get_last() function may help in this case.

In addition I think we should not throw an exception if $result is true.

So, I suppose, the final code for mailHandler should be the following:

public function mailHandler($to, $subject, $message, $headers, $parameters)
{
    if ($parameters === null) {
        $result = mail($to, $subject, $message, $headers);
    } else {
        $result = mail($to, $subject, $message, $headers, $parameters);
    }

    if (!$result) {
        $errstr = error_get_last()['message'];
        if (empty($errstr)) {
            $errstr = 'Unknown error';
        }
        throw new Exception\RuntimeException('Unable to send mail: ' . $errstr);
    }
}

Sorry, my qualification is not enough to complete all items you want to see here (regression test, CHANGELOG.md, etc.). But I hope my point of view may be useful.

Have a good day!

`error_get_last()` usage instead of error handler replacement.
@weierophinney
Copy link
Member

This repository has been closed and moved to laminas/laminas-mail; a new issue has been opened at laminas/laminas-mail#15.

@weierophinney
Copy link
Member

This repository has been moved to laminas/laminas-mail. If you feel that this patch is still relevant, please re-open against that repository, and reference this issue. To re-open, we suggest the following workflow:

  • Squash all commits in your branch (git rebase -i origin/{branch})
  • Make a note of all changed files (`git diff --name-only origin/{branch}...HEAD
  • Run the laminas/laminas-migration tool on the code.
  • Clone laminas/laminas-mail to another directory.
  • Copy the files from the second bullet point to the clone of laminas/laminas-mail.
  • In your clone of laminas/laminas-mail, commit the files, push to your fork, and open the new PR.
    We will be providing tooling via laminas/laminas-migration soon to help automate the process.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants