I wondered if the zeta components update would make this fix obsolete but adding
a test says not...
So this locks in the fix on
https://issues.civicrm.org/jira/browse/CRM-19215
// if its the activities that needs to be processed ..
try {
- $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail, $incomingMail->getAttachments(), $createContact, $emailFields);
+ $mailParams = CRM_Utils_Mail_Incoming::parseMailingObject($mail, $incomingMail->getAttachments(), $createContact, $emailFields, [$incomingMail->getFrom()]);
$activityParams = [
'activity_type_id' => (int) $dao->activity_type_id,
'campaign_id' => $dao->campaign_id ? (int) $dao->campaign_id : NULL,
}
}
}
-
$numAttachments = Civi::settings()->get('max_attachments_backend') ?? CRM_Core_BAO_File::DEFAULT_MAX_ATTACHMENTS_BACKEND;
for ($i = 1; $i <= $numAttachments; $i++) {
if (isset($mailParams["attachFile_$i"])) {
* @param $attachments
* @param bool $createContact
* @param array $emailFields
- * Which fields to process and create contacts for of from, to, cc, bcc
+ * Which fields to process and create contacts for - subset of [from, to, cc, bcc],
+ * @param array $from
*
* @return array
*/
- public static function parseMailingObject(&$mail, $attachments, $createContact = TRUE, $emailFields = ['from', 'to', 'cc', 'bcc']) {
-
- // Sometimes $mail->from is unset because ezcMail didn't handle format
- // of From header. CRM-19215.
- if (!isset($mail->from)) {
- if (preg_match('/^([^ ]*)( (.*))?$/', $mail->getHeader('from'), $matches)) {
- $mail->from = new ezcMailAddress($matches[1], trim($matches[2]));
- }
- }
-
+ public static function parseMailingObject(&$mail, $attachments, $createContact, $emailFields, $from) {
$params = [];
foreach ($emailFields as $field) {
// to, bcc, cc are arrays of objects, but from is an object, so make it an array of one object so we can handle it the same
if ($field === 'from') {
- $value = [$mail->$field];
+ $value = $from;
}
else {
$value = $mail->$field;
return $this->hash;
}
+ /**
+ * @return ezcMailAddress
+ */
+ public function getFrom(): ezcMailAddress {
+ return $this->mail->from;
+ }
+
/**
* @return string
*/
* @throws \CRM_Core_Exception
*/
public function __construct(ezcMail $mail, string $emailDomain, string $emailLocalPart) {
+ // Sometimes $mail->from is unset because ezcMail didn't handle format
+ // of From header. CRM-19215 (https://issues.civicrm.org/jira/browse/CRM-19215).
+ if (!isset($mail->from)) {
+ if (preg_match('/^([^ ]*)( (.*))?$/', $mail->getHeader('from'), $matches)) {
+ $mail->from = new ezcMailAddress($matches[1], trim($matches[2]));
+ }
+ }
$this->mail = $mail;
$this->body = CRM_Utils_Mail_Incoming::formatMailPart($mail->body, $this->attachments);
<?php
+use Civi\Api4\ActivityContact;
+
/**
* Class CRM_Utils_Mail_EmailProcessorInboundTest
* @group headless
* Test messed up from.
*
* This ensures fix for https://issues.civicrm.org/jira/browse/CRM-19215.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testBadFrom() :void {
$email = file_get_contents(__DIR__ . '/data/inbound/test_broken_from.eml');
+ $badEmails = [
+ 'foo@example.com' => 'foo@example.com (foo)',
+ "KO'Bananas@benders.com" => "KO'Bananas@benders.com",
+ ];
+ foreach ($badEmails as $index => $badEmail) {
+ $file = fopen(__DIR__ . '/data/mail/test_broken_from.eml' . $index, 'wb');
+ fwrite($file, str_replace('bad-email-placeholder', $badEmail, $email));
+ fclose($file);
+ }
- copy(__DIR__ . '/data/inbound/test_broken_from.eml', __DIR__ . '/data/mail/test_broken_from.eml');
$this->callAPISuccess('Job', 'fetch_activities', []);
+ $activities = ActivityContact::get()
+ ->addSelect('contact_id.email_primary.email', 'activity_id.activity_type_id:name', 'activity_id.subject')
+ ->addWhere('contact_id.email_primary.email', 'IN', array_keys($badEmails))
+ ->addWhere('record_type_id:name', '=', 'Activity Source')
+ ->execute();
+ $this->assertCount(2, $activities);
}
/**
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
-From: foo@example.com (foo)
+From: bad-email-placeholder
To: jjj@myorg.org
-Subject: This is for hooks
+Subject: Subject
Sample body for hook test.