Skip to content

Commit

Permalink
DIAM-1832 ignore email autoresponders
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kolomiiets committed Feb 7, 2017
1 parent 72c7eb7 commit 4e690fe
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Infrastructure/Message/Zend/AbstractMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class AbstractMessageProvider
/**
* @var \Symfony\Bridge\Monolog\Logger
*/
private $logger;
protected $logger;

/**
* Retrieves Message ID
Expand Down
72 changes: 71 additions & 1 deletion Infrastructure/Message/Zend/ImapMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Diamante\EmailProcessingBundle\Model\MessageProcessingException;
use Diamante\EmailProcessingBundle\Model\Service\EmailFilterService;
use Diamante\EmailProcessingBundle\Infrastructure\Message\Attachment;
use Zend\Mail\Header\HeaderInterface;
use Zend\Mail\Headers;

class ImapMessageProvider extends AbstractMessageProvider implements MessageProvider
{
Expand Down Expand Up @@ -72,17 +74,85 @@ public function fetchMessagesToProcess()
$messageAttachments = $this->processAttachments($imapMessage);
$isFailed = $this->isFailed($headers);
$isSystem = $this->isSystem($headers);
$isAutoresponder = $this->isAutoresponder($headers);
$recipients = $this->processRecipients($headers);

$messages[] = new Message($uniqueMessageId, $messageId, $messageSubject, $messageContent, $messageFrom,
$messageTo, $messageReference, $messageAttachments, $isFailed, $isSystem, $recipients);
$messageTo, $messageReference, $messageAttachments, $recipients, $isFailed, $isSystem, $isAutoresponder);
}
} catch (\Exception $e) {
$this->logger->addError($e->getMessage());
throw new MessageProcessingException($e->getMessage());
}
return $messages;
}

/**
* @param Headers $headers
* @return bool
*/
protected function isAutoresponder(Headers $headers)
{
$autoresponderHeaders = [
'Return-Path' => '<>',
'Preference' => 'auto_reply',
'X-Precedence' => 'auto_reply',
'X-Autorespond' => null,
'X-Autogenerated' => ['Forward', 'Group', 'Letter', 'Mirror', 'Redirect', 'Reply'],
'X-AutoReply-From' => null,
'X-Mail-Autoreply' => null,
'X-FC-MachineGenerated' => 'true',
'Auto-Submitted' => 'auto-replied',
'Precedence' => 'bulk',
'X-Autoreply' => 'yes',
'X-POST-MessageClass' => '9; Autoresponder',
'Delivered-To' => 'Autoresponder',
];

foreach ($autoresponderHeaders as $header => $value) {
$headerEntity = $headers->get($header);

if ($headerEntity instanceof \ArrayIterator) {
foreach ($headerEntity->getArrayCopy() as $k => $nestedHeader) {
if (!$nestedHeader instanceof HeaderInterface) {
throw new MessageProcessingException('Expected type header ArrayIterator or HeaderInterface');
}

if ($this->checkAutoresponder($nestedHeader, $value)) {
return true;
}
}
} elseif ($headerEntity instanceof HeaderInterface) {
if ($this->checkAutoresponder($headerEntity, $value)) {
return true;
}
} else {
throw new MessageProcessingException('Expected type header ArrayIterator or HeaderInterface');
}
}

return false;
}

/**
* @param HeaderInterface $genericHeader
* @param $predefinedValue
* @return bool
*/
protected function checkAutoresponder(HeaderInterface $genericHeader, $predefinedValue)
{
$headerValue = $genericHeader->getFieldValue();
if (is_null($predefinedValue)) {
return true;
} elseif (is_array($predefinedValue) && in_array($headerValue, $predefinedValue)) {
return true;
} elseif ($predefinedValue == $headerValue) {
return true;
}

return false;
}

/**
* @param \Zend\Mail\Headers $headers
*
Expand Down
44 changes: 30 additions & 14 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,24 @@ class Message
*/
private $isSystem;

/**
* @var bool
*/
private $isAutoresponder;

/**
* @param $uniqueId
* @param string|null $messageId
* @param string|null $subject
* @param string|null $content
* @param MessageSender $from
* @param string $to
* @param string|null $reference
* @param string|null $reference
* @param array $attachments
* @param array $recipients
* @param bool $isFailed
* @param bool $isSystem
* @param array $recipients
* @param bool $isAutoresponder
*/
public function __construct(
$uniqueId,
Expand All @@ -96,21 +102,23 @@ public function __construct(
$to,
$reference = null,
array $attachments = null,
$recipients = null,
$isFailed = false,
$isSystem = true,
$recipients = null
$isAutoresponder = false
) {
$this->uniqueId = $uniqueId;
$this->messageId = $messageId;
$this->subject = $subject;
$this->content = $content;
$this->from = $from;
$this->to = $to;
$this->reference = $reference;
$this->attachments = $attachments;
$this->isFailed = $isFailed;
$this->isSystem = $isSystem;
$this->recipients = $recipients;
$this->uniqueId = $uniqueId;
$this->messageId = $messageId;
$this->subject = $subject;
$this->content = $content;
$this->from = $from;
$this->to = $to;
$this->reference = $reference;
$this->attachments = $attachments;
$this->recipients = $recipients;
$this->isFailed = $isFailed;
$this->isSystem = $isSystem;
$this->isAutoresponder = $isAutoresponder;
}

/**
Expand Down Expand Up @@ -201,6 +209,14 @@ public function isSystem()
return $this->isSystem;
}

/**
* @return bool
*/
public function isAutoresponder()
{
return $this->isAutoresponder;
}

/**
* Retrieve emails of recipients
*
Expand Down
11 changes: 1 addition & 10 deletions Model/Service/MessageProcessingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function handle(MessageProvider $provider)
foreach ($strategies as $strategy) {
$this->processingContext->setStrategy($strategy);
try {
if (!$message->isFailed() && !$message->isSystem() && !$this->isEmailContainedInBlacklist($message)) {
if (!$message->isFailed() && !$message->isSystem() && !$message->isAutoresponder()) {
$this->processingContext->execute($message);
}

Expand All @@ -77,13 +77,4 @@ public function handle(MessageProvider $provider)
$provider->markMessagesAsProcessed($processedMessages);
}
}

public function isEmailContainedInBlacklist($message)
{
if (in_array($message->getFrom()->getEmail(), ['mailer-daemon@googlemail.com'])) {
return true;
}

return false;
}
}

0 comments on commit 4e690fe

Please sign in to comment.