Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AddressHeader instead of EmailAddressParser #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 0 additions & 129 deletions src/Email/EmailAddressParser.php

This file was deleted.

37 changes: 31 additions & 6 deletions src/EmailSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Externals\Email\Email;
use Externals\Email\EmailAddress;
use Externals\Email\EmailAddressParser;
use Externals\Email\EmailContentParser;
use Externals\Email\EmailRepository;
use Externals\Email\EmailSubjectParser;
Expand All @@ -17,6 +16,7 @@
use Rvdv\Nntp\Command\ArticleCommand;
use Rvdv\Nntp\Connection\Connection;
use Rvdv\Nntp\Exception\UnknownHandlerException;
use ZBateson\MailMimeParser\Header\AddressHeader;
use ZBateson\MailMimeParser\Header\DateHeader;
use ZBateson\MailMimeParser\MailMimeParser;
use ZBateson\MailMimeParser\Message;
Expand Down Expand Up @@ -143,17 +143,13 @@ public function synchronizeEmail(int $number, string $source)
$subject = $this->subjectParser->sanitize($parsedDocument->getHeaderValue('subject'));
$content = $this->contentParser->parse((string) $parsedDocument->getTextContent());

// We don't use the special AddressHeader class because it doesn't seem to parse the
// person's name at all
$fromHeader = $parsedDocument->getHeader('from');
if (!$fromHeader) {
$this->logger->warning("Cannot synchronize message $number because it contains no 'from' header");
return;
}
$emailAddressParser = new EmailAddressParser($fromHeader->getRawValue());
$fromArray = $emailAddressParser->parse();
/** @var EmailAddress $from */
$from = reset($fromArray);
$from = $this->getFirstFilteredAddress($fromHeader);

$emailId = $parsedDocument->getHeaderValue('message-id');

Expand Down Expand Up @@ -226,4 +222,33 @@ private function parseDateTime(Message $parsedDocument)

return $date;
}

/**
* @return EmailAddress
*/
private function getFirstFilteredAddress(AddressHeader $header)
{
$addresses = $header->getAddresses();
foreach ($addresses as $address) {
$email = $address->getEmail();
$email = preg_replace('/([. #]at[.# ])/', '@', $email);
$email = preg_replace('/([. #]dot[.# ])/', '.', $email);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Special cases for that one guy who
// put his whole resume as name and other
// marvelous joys
$name = $address->getName();
if (
mb_strpos($name, 'Watson Research') ||
mb_strlen($name) <= 3 ||
mb_strpos($name, '?') !== false ||
mb_strpos($name, 'http') !== false
) {
$name = '';
}
return new EmailAddress($name, $email);
}
}
return null;
}
}