Skip to content

Commit

Permalink
#1 support message/feedback-report
Browse files Browse the repository at this point in the history
  • Loading branch information
bashkarev committed Feb 24, 2017
1 parent 387fb95 commit f127f9e
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 6 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ $block->getMessage()
## message/rfc822
```php
$file = fopen('path/to/file.eml', 'r');
$main = \bashkarev\email\Parser::email($file);
$message = $main->getAttachments()[0]->getMessage();
$container = \bashkarev\email\Parser::email($file);
$message = $container->getAttachments()[0]->getMessage();
```

## message/feedback-report
```php
$file = fopen('path/to/file.eml', 'r');
$container = \bashkarev\email\Parser::email($file);
foreach ($container->getAttachments() as $attachment) {
if ($attachment->getMimeType() === 'message/feedback-report') {
/**
* @var \bashkarev\email\messages\Feedback $feedback
*/
$feedback = $attachment->getMessage();
$feedback->getType(); // Feedback::TYPE_ABUSE ...
}
}

```
4 changes: 2 additions & 2 deletions src/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function getMessage()
$this->message === null
&& $this->stream !== null
&& ($mime = $this->getMimeType()) !== null
&& ($mime === 'message/rfc822' || $mime === 'message/partial')
&& strncmp($mime, 'message/', 8) === 0
) {
$this->message = (new Email())->parse($this->stream->getHandle(), false);
$this->message = (new Email($mime))->parse($this->stream->getHandle(), false);
}
return $this->message;
}
Expand Down
7 changes: 7 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ class Parser
{
public static $buffer = 500000;
public static $charset = 'UTF-8';
/**
* mime message class map
* @var array
*/
public static $map = [
'message/feedback-report' => 'bashkarev\email\messages\Feedback'
];

/**
* @param mixed $handles
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Address
/**
* @param $text
* @param $charset
* @return array
* @return \bashkarev\email\Address[]
*/
public static function parse($text, $charset)
{
Expand Down
144 changes: 144 additions & 0 deletions src/messages/Feedback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* @copyright Copyright (c) 2017 Dmitriy Bashkarev
* @license https://github.com/bashkarev/email/blob/master/LICENSE
* @link https://github.com/bashkarev/email#readme
*/

namespace bashkarev\email\messages;

use bashkarev\email\helpers\Address;
use bashkarev\email\Message;

/**
*
* @see https://tools.ietf.org/html/rfc5965
* @author Dmitriy Bashkarev <dmitriy@bashkarev.com>
*/
class Feedback extends Message
{
/**
* spam or some other kind of email abuse;
*/
const TYPE_ABUSE = 'abuse';
/**
* indicates some kind of fraud or phishing activity;
*/
const TYPE_FRAUD = 'fraud';
/**
* report of a virus found in the originating message;
*/
const TYPE_VIRUS = 'virus';
/**
* any other feedback that doesn't fit into other types;
*/
const TYPE_OTHER = 'other';
/**
* can be used to report an email message that was mistakenly marked as spam
*/
const TYPE_NOT_SPAM = 'not-spam';

/**
* @return null|string
*/
public function getType()
{
if (!$this->hasHeader('feedback-type')) {
return null;
}
return $this->getHeaderLine('feedback-type');
}

/**
* @return null|string
*/
public function getUserAgent()
{
if (!$this->hasHeader('user-agent')) {
return null;
}
return $this->getHeaderLine('user-agent');
}

/**
* @return null|string
*/
public function getVersion()
{
if (!$this->hasHeader('version')) {
return null;
}
return $this->getHeaderLine('version');
}

/**
* @return null|string
*/
public function getOriginalEnvelopeId()
{
if (!$this->hasHeader('original-envelope-id')) {
return null;
}
return $this->getHeaderLine('Original-Envelope-Id');
}

/**
* @return \bashkarev\email\Address|null
*/
public function getOriginalMailFrom()
{
$from = Address::parse($this->getHeaderLine('Original-Mail-From'), $this->getCharset());
if ($from === []) {
return null;
}
return $from[0];
}

/**
* @return \DateTime|null
*/
public function getArrivalDate()
{
if (!$this->hasHeader('arrival-date')) {
return null;
}

try {
$date = new \DateTime($this->getHeaderLine('arrival-date'));
} catch (\Exception $e) {
return null;
}
return $date;
}

/**
* @return array
*/
public function getReportingMTA()
{
return $this->getHeader('reporting-mta');
}

/**
* @return null|string
*/
public function getSourceIP()
{
if (!$this->hasHeader('source-ip')) {
return null;
}
return $this->getHeaderLine('source-ip');
}

/**
* @return int|null
*/
public function getIncidents()
{
if (!$this->hasHeader('incidents')) {
return null;
}
return (int)$this->getHeader('incidents');
}

}
8 changes: 7 additions & 1 deletion src/parser/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace bashkarev\email\parser;

use bashkarev\email\Message;
use bashkarev\email\Parser;
use bashkarev\email\Part;

/**
Expand Down Expand Up @@ -41,6 +42,12 @@ class Email
*/
protected $line;

public function __construct($mime = null)
{
$class = ($mime !== null && isset(Parser::$map[$mime])) ? Parser::$map[$mime] : 'bashkarev\email\Message';
$this->message = new $class();
}

/**
* @param resource|string|array $handles
* @param boolean $afterClose
Expand All @@ -51,7 +58,6 @@ public function parse($handles, $afterClose = true)
if (!is_array($handles)) {
$handles = [$handles];
}
$this->message = new Message();
foreach ($handles as $handle) {
if (!is_resource($handle)) {
$this->handle = fopen('php://memory', 'r+');
Expand Down
118 changes: 118 additions & 0 deletions tests/fixtures/complaints/aol.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
Content-Type: multipart/report; report-type="feedback-report";
boundary="===============8110170601564424224=="
MIME-Version: 1.0
Authentication-Results: mx.google.com;
spf=neutral (google.com: 209.85.160.174 is neither
permitted nor denied by best guess record for domain of
mike+caf_=bob=gmail.com@example.com)
smtp.mail=mike+caf_=bob=gmail.com@example.com
Date: Wed, 17 Nov 2010 15:02:19 EST
Delivered-To: bob@example.com
From: scomp@aol.net
Message-Id: <201011171502.fa984ce434cc97@omr-m11.mx.aol.com>
Precedence: list
Received: from mail-iw0-f179.google.com (mail-iw0-f179.google.com
[209.85.214.179])
by mxa.example.com (Postfix) with ESMTP id 4730DF01123
for <bob@example.net>; Wed, 17 Nov 2010 20:02:42 +0000 (UTC)
Received-Spf: neutral (google.com: 209.85.160.174 is neither permitted nor
denied by best guess record for domain of
mike+caf_=bob=gmail.com@example.com)
client-ip=209.85.160.174;
Sender: SRS0=zFhZ=SS=aol.net=scomp@example.com
Subject: Email Feedback Report for IP 173.193.210.34
To: spam.abuse@example.com
X-Aol-Inrly: mars-34.example.com [173.193.210.34] scmp-d48
X-Aol-Ip: 172.19.132.211
X-Beenthere: admin@example.com
X-Forwarded-For: bob@example.com bob@example.net
X-Forwarded-To: bob@example.net
X-Loop: scomp
X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain
of bc=scomp+aol.net=@example.com designates
174.37.214.195 as permitted
sender) smtp.mail=bc=scomp+aol.net=@example.com;
dkim=pass header.i=@example.com
X-Original-Sender: scomp@aol.net
X-X-Forwarded-For: mike@example.com bob@example.com

--===============8110170601564424224==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
This is an email abuse report for an email message with the message-id of 20101117174130.28884.55421@luna received from IP address 173.193.210.34 on Wed, 17 Nov 2010 12:41:30 -0500 (EST)
For information, please review the top portion of the following page:
http://postmaster.aol.com/tools/fbl.html
For information about AOL E-mail guidelines, please see
http://postmaster.aol.com/guidelines/
If you would like to cancel or change the configuration for your FBL please use the tool located at:
http://postmaster.aol.com/waters/fbl_change_form.html
--===============8110170601564424224==
Content-Type: message/feedback-report
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Feedback-Type: abuse
Received-Date: Wed, 17 Nov 2010 12:41:30 -0500 (EST)
Redacted-Address: redacted
Reported-Domain: mars-34.example.com
Source-Ip: 173.193.210.34
User-Agent: AOL SComp
Version: 0.1
Content-Transfer-Encoding: 7bit
--===============8110170601564424224==
Content-Type: message/rfc822
MIME-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Date: Wed, 17 Nov 2010 17:41:30 +0000
Dkim-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=example.com; q=dns/txt;
s=mg;
t=1290015690; h=Content-Type: MIME-Version: Content-Transfer-Encoding:
To: From: Date: Subject: Message-ID: MIME-Version;
bh=nxbn1zWL9sK6KsPWON+E7/RL1hjKmMs7ob7EC3BfTYQ=;
b=Uh281hr4hRrfANPsK2KNUWrKzz5h22EWSMcmE51qm00AR1cbtjHMU/exHhkEQ1VcCxl4FnVn
ZPWMq8OSBtKhycMa4sSxoMOS7cjxKMN23F4KG/cw/G8UUD6kYWTu8u+g9AX539uNuO1ohPZk
OLGQFkwKgxbQGmNWu9dsxKv43Ms=
Domainkey-Signature: a=rsa-sha1; c=nofws; d=example.com; s=mg;
h=Content-Type: MIME-Version: Content-Transfer-Encoding: To: From:
Date: Subject: Message-ID: MIME-Version;
b=gWffI33O+PS/Sti8uxIAqNP3SHzc5uPBPJB/R3dZdQ+Uu2n/CmfWJTPfYTO0u83MdkbLD6
/SYeFYEnQl26vto/nE7GWpPuwtPeuxm8B11S1eUE178I3As3a2V0QKxC8T9CCX0PmfOLuoKc
ErBo/lweMX6ZFVzutgNIQrEn5qHJk=
From: Bob <bob@example.com>
Message-Id: <20101117174130.28884.55421@luna>
Received: from mars-34.example.com (mars-34.example.com [173.193.210.34])
by mtain-mb01.r1000.mx.aol.com (Internet Inbound) with ESMTP id
6C40F380000A4
for <redacted@aol.com>; Wed, 17 Nov 2010 12:41:30 -0500 (EST)
Subject: exciting Repair the transmittion
To: redacted@aol.com
X-Aol-Global-Disposition: G
X-Aol-Ip: 173.193.210.34
X-Aol-Scoll-Authentication: mail_rly_antispam_dkim-d309.2 ;
domain : example.com DKIM : pass
X-Aol-Scoll-Score: 0:2:344886368:93952408
X-Aol-Scoll-Url_count: 0
X-Aol-Sid: 3039ac1d60154ce413ca6e64
Content-Transfer-Encoding: 7bit
Dude
Hello
--
Best,
Bob


--===============8110170601564424224==--
Loading

0 comments on commit f127f9e

Please sign in to comment.