diff --git a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php index 120d9fa17f..88d0053b47 100644 --- a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php +++ b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/AfterFetch.php @@ -31,7 +31,6 @@ namespace Espo\Core\Mail\Account\GroupAccount\Hooks; use Espo\Core\Name\Field; use Espo\Core\Name\Link; -use Laminas\Mail\Message; use Espo\Core\Mail\Account\GroupAccount\AccountFactory as GroupAccountFactory; use Espo\Core\Mail\SenderParams; @@ -97,7 +96,7 @@ class AfterFetch implements AfterFetchInterface } if ($account->createCase()) { - if ($beforeFetchResult->get('isAutoReply')) { + if ($beforeFetchResult->get('isAutoSubmitted')) { return; } @@ -117,7 +116,10 @@ class AfterFetch implements AfterFetchInterface } if ($account->autoReply()) { - if ($beforeFetchResult->get('skipAutoReply')) { + if ( + $beforeFetchResult->get('isAutoSubmitted') || + $beforeFetchResult->get('skipAutoReply') + ) { return; } diff --git a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/BeforeFetch.php b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/BeforeFetch.php index 61da506be0..97bfdbbeb4 100644 --- a/application/Espo/Core/Mail/Account/GroupAccount/Hooks/BeforeFetch.php +++ b/application/Espo/Core/Mail/Account/GroupAccount/Hooks/BeforeFetch.php @@ -75,7 +75,7 @@ class BeforeFetch implements BeforeFetchInterface return BeforeFetchResult::create() ->with('skipAutoReply', $this->checkMessageCannotBeAutoReplied($message)) - ->with('isAutoReply', $this->checkMessageIsAutoReply($message)); + ->with('isAutoSubmitted', $this->checkMessageIsAutoSubmitted($message)); } private function processBounced(Message $message): bool @@ -127,16 +127,23 @@ class BeforeFetch implements BeforeFetchInterface private function checkMessageIsAutoReply(Message $message): bool { + if ($this->checkMessageIsAutoSubmitted($message)) { + return true; + } + return $this->autoReplyDetector->detect($message); } private function checkMessageCannotBeAutoReplied(Message $message): bool { - if ($message->getHeader('X-Auto-Response-Suppress') === 'AutoReply') { + if ( + $message->getHeader('X-Auto-Response-Suppress') === 'AutoReply' || + $message->getHeader('X-Auto-Response-Suppress') === 'All' + ) { return true; } - if ($message->getHeader('X-Auto-Response-Suppress') === 'All') { + if ($this->checkMessageIsAutoSubmitted($message)) { return true; } @@ -146,4 +153,14 @@ class BeforeFetch implements BeforeFetchInterface return false; } + + private function checkMessageIsAutoSubmitted(Message $message): bool + { + if ($this->autoReplyDetector->detect($message)) { + return true; + } + + return $message->getHeader('Auto-Submitted') && + strtolower($message->getHeader('Auto-Submitted')) !== 'no'; + } } diff --git a/application/Espo/Core/Mail/Importer/DefaultAutoReplyDetector.php b/application/Espo/Core/Mail/Importer/DefaultAutoReplyDetector.php index eac8e75cfa..bafd486c6b 100644 --- a/application/Espo/Core/Mail/Importer/DefaultAutoReplyDetector.php +++ b/application/Espo/Core/Mail/Importer/DefaultAutoReplyDetector.php @@ -44,8 +44,8 @@ class DefaultAutoReplyDetector implements AutoReplyDetector } if ( - $message->getHeader('Auto-submitted') && - strtolower($message->getHeader('Auto-submitted')) !== 'no' + $message->getHeader('Auto-Submitted') && + strtolower($message->getHeader('Auto-Submitted')) === 'auto-replied' ) { return true; } diff --git a/application/Espo/Core/Mail/Sender.php b/application/Espo/Core/Mail/Sender.php index b30d54e7f5..fe41d85148 100644 --- a/application/Espo/Core/Mail/Sender.php +++ b/application/Espo/Core/Mail/Sender.php @@ -305,7 +305,7 @@ class Sender $params = array_merge($this->params, $this->overrideParams); - $this->applyHeaders($message); + $this->applyHeaders($email, $message); $this->applyFrom($email, $message, $params); $this->addRecipientAddresses($email, $message); $this->applyReplyTo($email, $message, $params); @@ -589,7 +589,7 @@ class Sender $message->subject($email->getSubject() ?? ''); } - private function applyHeaders(Message $message): void + private function applyHeaders(Email $email, Message $message): void { foreach ($this->headers as $item) { $message->getHeaders()->addTextHeader($item[0], $item[1]); @@ -605,6 +605,10 @@ class Sender $message->getHeaders()->addTextHeader($it->getFieldName(), $it->getFieldValue()); } } + + if ($email->isAutoReply() && !$message->getHeaders()->has('Auto-Submitted')) { + $message->getHeaders()->addTextHeader('Auto-Submitted', 'auto-replied'); + } } private function prepareEnvelope(Message $message): ?Envelope diff --git a/application/Espo/Modules/Crm/Tools/MassEmail/DefaultMessageHeadersPreparator.php b/application/Espo/Modules/Crm/Tools/MassEmail/DefaultMessageHeadersPreparator.php index f06a1bec79..829d70c71a 100644 --- a/application/Espo/Modules/Crm/Tools/MassEmail/DefaultMessageHeadersPreparator.php +++ b/application/Espo/Modules/Crm/Tools/MassEmail/DefaultMessageHeadersPreparator.php @@ -43,6 +43,7 @@ class DefaultMessageHeadersPreparator implements MessageHeadersPreparator { $headers->addTextHeader('X-Queue-Item-Id', $data->getId()); $headers->addTextHeader('Precedence', 'bulk'); + $headers->addTextHeader('Auto-Submitted', 'auto-generated'); $this->addMandatoryOptOut($headers, $data); } diff --git a/application/Espo/Tools/EmailNotification/Processor.php b/application/Espo/Tools/EmailNotification/Processor.php index 80394f4e35..59aa90c3c3 100644 --- a/application/Espo/Tools/EmailNotification/Processor.php +++ b/application/Espo/Tools/EmailNotification/Processor.php @@ -339,8 +339,7 @@ class Processor $subject = $this->getHtmlizer()->render($note, $subjectTpl, 'mention-email-subject', $data, true); $body = $this->getHtmlizer()->render($note, $bodyTpl, 'mention-email-body', $data, true); - /** @var Email $email */ - $email = $this->entityManager->getNewEntity(Email::ENTITY_TYPE); + $email = $this->entityManager->getRDBRepositoryByClass(Email::class)->getNew(); $email ->setSubject($subject) @@ -365,10 +364,15 @@ class Processor } } + $sender = $this->emailSender + ->withParams($senderParams); + + if ($note->getType() !== Note::TYPE_POST) { + $sender = $sender->withAddedHeader('Auto-Submitted', 'auto-generated'); + } + try { - $this->emailSender - ->withParams($senderParams) - ->send($email); + $sender->send($email); } catch (Exception $e) { $this->log->error("Email notification: {$e->getMessage()}", ['exception' => $e]); } diff --git a/application/Espo/Tools/LeadCapture/ConfirmationSender.php b/application/Espo/Tools/LeadCapture/ConfirmationSender.php index f96b436dd7..f46a811221 100644 --- a/application/Espo/Tools/LeadCapture/ConfirmationSender.php +++ b/application/Espo/Tools/LeadCapture/ConfirmationSender.php @@ -218,6 +218,7 @@ class ConfirmationSender } $sender + ->withAddedHeader('Auto-Submitted', 'auto-generated') ->withAttachments($emailData->getAttachmentList()) ->send($email); } diff --git a/application/Espo/Tools/UserSecurity/Password/Sender.php b/application/Espo/Tools/UserSecurity/Password/Sender.php index 24ea5ca28c..6c20f12d85 100644 --- a/application/Espo/Tools/UserSecurity/Password/Sender.php +++ b/application/Espo/Tools/UserSecurity/Password/Sender.php @@ -88,7 +88,9 @@ class Sender ->setSubject($subject) ->setBody($body); - $this->createSender()->send($email); + $this->createSender() + ->withAddedHeader('Auto-Submitted', 'auto-generated') + ->send($email); } /** @@ -127,7 +129,9 @@ class Sender ->setBody($body) ->addToAddress($emailAddress); - $this->createSender()->send($email); + $this->createSender() + ->withAddedHeader('Auto-Submitted', 'auto-generated') + ->send($email); } private function createSender(): EmailSenderSender