aghstrategies / com.aghstrategies.airmail

Unified CiviCRM bounce event handler for SMTP services
Other
8 stars 13 forks source link

Airmail fails to parse $event->civimail_source coming back from SendGrid. #45

Closed bugfolder closed 8 months ago

bugfolder commented 8 months ago

Using CiviCRM 5.70.2, Airmail 2.1, with PHP 8.1 in BackdropCMS 1.27.0 (all latest versions, recently updated).

After sending a large mailing, the watchdog log is filling up with PHP warnings like this:

Warning: Trying to access array offset on value of type null in CRM_Airmail_Backend_Sendgrid->processMessages() (line 25 of /mysite/civi-extensions/com.aghstrategies.airmail/CRM/Airmail/Backend/Sendgrid.php).

There is a triplet of warnings for lines 25, 26, and 27. Actually, there are many such triplets (it was a large mailing).

Some "hot" debugging gives some indication of what's going on. At line 22, we have this:

      $mailingJobInfo = E::parseSourceString($event->civimail_source);

That turns out to end up with the value NULL, which gives rise to the three warnings above (and I'm not sure what other problems might be happening downstream).

A typical value of $event->civimail_source is this: bouncesb.97.119993.b6sakmshrynbsghr@MYSITE.org (substituting MYSITE for the actual domain in this example).

All the other fields in $event are present; here's the full structure (with a bit of anonymization):

$event = (object) array( 'civimail_source' => 'bouncesb.97.119993.b6sakmshrynbsghr@MYSITE', 'email' => 'info@THEIRDOMAIN.org', 'event' => 'open', 'ip' => 'THEIR IP', 'sg_content_type' => 'html', 'sg_event_id' => 'YTmZ7oXNRO662v2EYsbWZw', 'sg_machine_open' => true, 'sg_message_id' => '48J9ddwlT2ute8JfbFymyA.recvd-6449d6bd6c-spx68-1-65E6E29D-B.0', 'timestamp' => 1709672044, 'useragent' => 'Mozilla/5.0', )

So it appears that CRM_Airmail_Utils::parseSourceString() just isn't able to handle the current version of the civimail_source string.

I don't recall seeing this blast of watchdog warnings in previous mailings. Could the format of this string have changed in a recent version of CiviCRM? or SendGrid?

bugfolder commented 8 months ago

I can add a little bit to this. When parseSourceString() attempts to parse $civimail_source, it is using the regex

$regex = '/^bounces(b|c|e|o|r|u)\\.(\\d+)\\.(\\d+)\\.([0-9a-f]{16})@MYSITE\\.org$/';

And so this part of the regex ([0-9a-f]{16}) fails to match the string b6sakmshrynbsghr in the example above. That string gets returned as $bounceEvent['hash'], so it's a hash of something. Could it be that the hash function changed?

Hmmm...Changing that bit of the regex to ([0-9a-z]{16}) seems to stop the warnings, which suggests that the events are now being processed correctly.

bugfolder commented 8 months ago

Yes. That change stops the warnings and opens are being recording in the mailing report. PR to come shortly.

bugfolder commented 8 months ago

Ah, I see that this is already done in this commit: d346a42df3a404cb0ca655f265784d7843c9651a

bugfolder commented 8 months ago

And I see there's a release version 2.2.2 that includes this. Would be nice if CiviCRM didn't claim that version 2.1 was "up to date."

image