magepal / magento2-gmail-smtp-app

Configure Magento 2 to send email using Google App, Gmail, Amazon Simple Email Service (SES), Microsoft Office365 and many other SMTP (Simple Mail Transfer Protocol) servers
https://www.magepal.com
319 stars 148 forks source link

$proceed() is not always called avoiding other plugins to be executed #186

Closed r-martins closed 3 months ago

r-martins commented 2 years ago

As we can see on line 75 of TransportPlugin.php, the $proceed() is not always called.

    public function aroundSendMessage(
        TransportInterface $subject,
        Closure $proceed
    ) {
        if ($this->dataHelper->isActive()) {
            if (method_exists($subject, 'getStoreId')) {
                $this->storeModel->setStoreId($subject->getStoreId());
            }

            $message = $subject->getMessage();

            if ($message instanceof Message || $message instanceof EmailMessageInterface) {
                $smtp = $this->smtpFactory->create(
                    ['dataHelper' => $this->dataHelper, 'storeModel' => $this->storeModel]
                );
                $smtp->sendSmtpMessage($message);
            } else {
                $proceed();
            }
        } else {
            $proceed();
        }
    }

Quoting the around plugin docs:

If the around method does not call the callable, it will prevent the execution of all the plugins next in the chain and the original method call.

I still haven't confirmed, but this can result in problems related to core saving address plugins and later avoiding the customer to place a second order using the same address.

HenKun commented 1 year ago

@r-martins $proceed must not be called in every branch, that is the sense of the around plugin. The original method and around plugins following this very plugin are not called, which is correct. However, before and after plugins are not touched by this behavior. This is where the Magento docs might be misleading. So the behavior is intended and correct.