Skip to content

Commit 759e799

Browse files
committed
proof-of-concept done
1 parent c63aca4 commit 759e799

File tree

6 files changed

+103
-24
lines changed

6 files changed

+103
-24
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ matrix:
2121

2222
before_install:
2323
- sudo apt-get -y -qq update
24-
# - sudo apt-get -y -qq install fontconfig libxrender1 xfonts-base xfonts-75dpi
24+
- if [[ "$TRAVIS_PHP_VERSION" = "7.0" ]]; then composer require --dev --no-update phpunit/phpunit ~5 ; fi
2525

2626
before_script:
27+
- if [[ "$TRAVIS_PHP_VERSION" = "5.5" ]]; then pecl install mailparse-2.1.6 ; fi
28+
- if [[ "$TRAVIS_PHP_VERSION" = "5.6" ]]; then pecl install mailparse-2.1.6 ; fi
29+
- if [[ "$TRAVIS_PHP_VERSION" = "7.0" ]]; then pecl install mailparse ; fi
2730
- composer self-update
2831
- composer install --no-interaction
2932

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
}
1616
],
1717
"require": {
18-
"php": ">=5.5.0",
19-
"symfony/yaml": "^3.1"
18+
"php": "^5.5.0 || ^7.0",
19+
"ext-mailparse": "*",
20+
"symfony/yaml": "^3.1",
21+
"php-mime-mail-parser/php-mime-mail-parser": "^2.5"
2022
},
2123
"require-dev": {
2224
"phpunit/phpunit": "~4.8",

src/Detector.php

+43-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
namespace Cwd\BounceDetection;
12+
use PhpMimeMailParser\Parser;
1213

1314
/**
1415
* Class Detector
@@ -35,23 +36,61 @@ public function __construct($ruleFile = null)
3536
*/
3637
public function testAll($mail)
3738
{
38-
$result = $this->testRulesAgainst($this->ruleConfig->getBodyRules(), $mail);
39+
$result = $this->testRulesAgainst($this->ruleConfig->getDSNRules(), $mail);
3940

4041
if ($result === null) {
41-
$result = $this->testRulesAgainst($this->ruleConfig->getDSNRules(), $mail);
42+
$result = $this->testRulesAgainst($this->ruleConfig->getBodyRules(), $mail);
4243
}
4344

45+
if ($result === null) {
46+
47+
}
48+
49+
$result['email'] = $this->findEmail($mail);
50+
4451
return $result;
4552
}
4653

54+
/**
55+
* @param $mail
56+
*
57+
* @return string
58+
*/
59+
public function findEmail($mail)
60+
{
61+
foreach ($this->ruleConfig->getEmailRules() as $rule) {
62+
if (preg_match($rule['regexp'], $mail, $match)) {
63+
return $match[1];
64+
}
65+
}
66+
67+
return null;
68+
}
69+
70+
/**
71+
* @param array $rules
72+
* @param string $mail
73+
*
74+
* @return array
75+
*/
4776
public function testRulesAgainst(array $rules, $mail)
4877
{
78+
$parser = new Parser();
79+
$parser->setText($mail);
80+
$data = $parser->getData();
81+
4982
foreach ($rules as $rule) {
50-
if (preg_match($rule['regexp'], $mail, $match)) {
51-
return [
83+
if (preg_match($rule['regexp'], $data, $match)) {
84+
$result = [
5285
'rule_cat' => $rule['category'],
5386
'rule_no' => $rule['number'],
5487
];
88+
89+
if (isset($rule['idx_email'])) {
90+
$result['email'] = $match[$rule['idx_email']];
91+
}
92+
93+
return $result;
5594
}
5695
}
5796
}

src/RuleConfig.php

+17
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public function getDSNRules()
4848
return (isset($this->rules['dsn'])) ? $this->rules['dsn'] : null;
4949
}
5050

51+
/**
52+
* @return array
53+
*/
54+
public function getEmailRules()
55+
{
56+
return (isset($this->rules['email'])) ? $this->rules['email'] : null;
57+
}
58+
59+
/**
60+
*
61+
* @return string|null
62+
*/
63+
public function getDiagnosticRegExp()
64+
{
65+
return isset($this->rules['diagnostic']) ? $this->rules['diagnostic'] : null;
66+
}
67+
5168
/**
5269
* @return \ArrayIterator
5370
*/

src/rules.yml

+11-8
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,14 @@ dsn:
561561
category: 'delayed'
562562
number: '0213'
563563

564+
- regexp: '/Delivery to the following recipient has been delayed/is'
565+
category: 'delayed'
566+
number: '0214'
567+
568+
- regexp: '/Delivery to the following recipient failed permanently/is'
569+
category: 'unknown'
570+
number: '0245'
571+
564572
- regexp: '/(?:alias|account|recipient|address|email|mailbox|user)(.*)invalid/i'
565573
category: 'unknown'
566574
number: '0107'
@@ -658,14 +666,9 @@ email:
658666
- regexp: '/Final-Recipient: rfc822;(.*)/i'
659667
- regexp: '/quota exceed.*<(\S+@\S+\w)>/is'
660668

661-
action:
662-
- regexp: '/Action: (.+)/i'
663-
664-
status:
665-
- regexp: '/Status: ([0-9\.]+)/i'
666-
667-
diagnostic:
668-
- regexp: '/Diagnostic-Code:((?:[^\n]|\n[\t ])+)(?:\n[^\t ]|$)/is'
669+
action: '/Action: (.+)/i'
670+
status: '/Status: ([0-9\.]+)/i'
671+
diagnostic: '/Diagnostic-Code:((?:[^\n]|\n[\t ])+)(?:\n[^\t ]|$)/is'
669672

670673

671674

tests/BounceTest.php

+24-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@
2121
*/
2222
class BounceTest extends \PHPUnit_Framework_TestCase
2323
{
24+
25+
protected $falsePositiv = [
26+
'tests/fixtures/bounce-email/tt_1234210666.txt',
27+
'tests/fixtures/bounce-email/tt_1234211024.txt',
28+
'tests/fixtures/bouncehammer/cannot-parse.eml',
29+
'tests/fixtures/tmail_bouncer/legit_multipart.eml',
30+
'tests/fixtures/tmail_bouncer/yahoo_legit.eml',
31+
'tests/fixtures/tmail_bouncer/out_of_office.eml',
32+
'tests/fixtures/PHP-Bounce-Handler/46.eml',
33+
'tests/fixtures/PHP-Bounce-Handler/29.eml',
34+
'tests/fixtures/mail-deliverystatus-bounceparser/spam-with-image.msg',
35+
'tests/fixtures/mail-deliverystatus-bounceparser/aol-senderblock.msg',
36+
'tests/fixtures/mail-deliverystatus-bounceparser/polish-autoreply.msg',
37+
'tests/fixtures/mail-deliverystatus-bounceparser/spam-lots-of-bogus-addresses.msg',
38+
'tests/fixtures/mail-deliverystatus-bounceparser/aol-vacation.msg',
39+
'tests/fixtures/mail-deliverystatus-bounceparser/spam-with-badly-parsed-email.msg',
40+
'tests/fixtures/mail-deliverystatus-bounceparser/bluebottle.msg',
41+
'tests/fixtures/mail-deliverystatus-bounceparser/no-message-collected.msg',
42+
'tests/fixtures/mail-deliverystatus-bounceparser/autoreply.msg',
43+
'tests/fixtures/node-baunsu/encoded_spam.txt',
44+
];
45+
2446
/**
2547
* @var Detector
2648
*/
@@ -53,16 +75,9 @@ public function testMail($fileName)
5375

5476
$result = self::$detector->testAll($mail);
5577

56-
/*
57-
if ($result === null) {
58-
dump([$fileName => '-- Nothing found --']);
59-
} else {
60-
dump([$fileName => $result]);
78+
if (!in_array($fileName, $this->falsePositiv)) {
79+
$this->assertEquals(3, count($result));
6180
}
62-
*/
63-
64-
$this->assertEquals(true, is_array($result));
65-
6681
}
6782

6883
/**

0 commit comments

Comments
 (0)