Fixed Error with Emogrifier on PHP 8.0

mattrogowski

Well-known member
Affected version
2.2.4
When trying to send emails with 2.2.4 on PHP, the following error is triggered:

ArgumentCountError: Too few arguments to function Pelago\Emogrifier::handleXpathQueryWarnings(), 4 passed and exactly 5 expected in src/vendor/pelago/emogrifier/src/Emogrifier.php at line 1783

I can see this is a package included inside ./src/vendor, but having looked on their Github, it seems like the packaged version is quite old - it's in their 3.1 branch which hasn't been updated for 2 and a half years, whereas their main branch seems to be completely different.

I'm also confused as to how this hasn't been reported before or fixed already - surely this would be affecting all emails? 🧐 🤔 I'm using the default email style.
 
I'm pretty sure this is really just masking another error, as that's erroring about it trying to call an error handler function (which would normally itself be triggering an exception).

Saying that, the error handler does seem to use the now-removed context elements, though I'm pretty sure it would fall back to our error handler which would throw an exception anyway.

So all that to say that there is probably some other error being triggered based on the HTML or CSS rules in your setup, but this does need to be fixed.
 
Yeah, I was about to add that it's indicative of a separate issue triggering the handler in the first place, but would probably give a clearer error if it was actually being handled. The error handle erroring itself is quite inconvenient 😂

I haven't changed any HTML or CSS relating to the emails that I'm aware of, but I'll try and pinpoint the specific rule that's causing the initial error.
 
Okay, so after fixing the PHP 8.0 incompatibility with the error handler I can see the style issue is coming from one of our addons. That explains why it's not a more widespread issue at least.

However, there is then still an error output which breaks the javascript response:

Code:
<br />
<b>Warning</b>:  DOMXPath::query(): Invalid predicate in <b>/Users/mattrogowski/Sites/xf2/src/vendor/pelago/emogrifier/src/Emogrifier.php</b> on line <b>494</b><br />
<br />
<b>Warning</b>:  DOMXPath::query(): Invalid expression in <b>/Users/mattrogowski/Sites/xf2/src/vendor/pelago/emogrifier/src/Emogrifier.php</b> on line <b>494</b><br />
<br />
<b>Warning</b>:  foreach() argument must be of type array|object, bool given in <b>/Users/mattrogowski/Sites/xf2/src/vendor/pelago/emogrifier/src/Emogrifier.php</b> on line <b>503</b><br />
{
    "status": "ok",
    "message": "Your changes have been saved.",
    "redirect": "https://xf2.build/admin.php?api-keys/",
    "visitor": {
        "conversations_unread": "0",
        "alerts_unviewed": "0",
        "total_unread": "0"
    },
    "debug": {
        "time": 0.1861,
        "queries": 16,
        "memory": 14.43
    }
}

So the cause of the error isn't an XF issue, but the fact the Emogrifier isn't PHP 8.0 compatible means that if there is an error, it's not handled for the end user properly. I imagine the newer version of their library would handle the errors differently?
 
Last edited:
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.5).

Change log:
Correctly handle errors when inlining CSS into emails with PHP 8.
There may be a delay before changes are rolled out to the XenForo Community.
 
Top Bottom