Skip to content

Commit 3b83bb9

Browse files
committed
New return structure
1 parent 23c382f commit 3b83bb9

File tree

5 files changed

+44
-10
lines changed

5 files changed

+44
-10
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# 📧 Email Structure Parser
2-
Given an IMAP stream and a message number, this library will parse the
1+
# Email Structure Parser
2+
Given an IMAP stream and a message nubmer, this library will parse the
33
structure of multipart emails.
44

55
## Installation
@@ -43,7 +43,7 @@ $parts = $parser->getParts();
4343
var_dump($parts['TEXT/HTML']);
4444

4545
// Save attached PNG images
46-
foreach($parts['IMAGE/PNG'] as $key => $image) {
47-
file_put_contents($key.'.png', $image);
46+
foreach($parts['IMAGE/PNG'] as $image) {
47+
file_put_contents($image->name, $image->content);
4848
}
49-
```
49+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "divineomega/email-structure-parser",
3-
"description": "Given an IMAP stream and a message number, this library will parse the structure of multipart emails.",
3+
"description": "Email Structure Parser",
44
"type": "library",
55
"license": "LGPL-3.0-only",
66
"authors": [

src/EmailStructureParser.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,37 @@ private function getPart(&$returnParts, $structure, $partNumber = false)
8282
else
8383
{
8484
$mimeType = $this->getMimeType($substructure);
85-
$returnParts[$mimeType][] = $this->getPartByPartNumber($substructure, $partToGet);
85+
$name = $this->getParameterValue($substructure, 'name');
86+
$content = $this->getPartByPartNumber($substructure, $partToGet);
87+
$returnParts[$mimeType][] = new Part($name, $content);
8688
}
8789
}
8890
}
8991
else
9092
{
9193
$mimeType = $this->getMimeType($structure);
92-
$returnParts[$mimeType][] = $this->getPartByPartNumber($structure, 1);
94+
$name = $this->getParameterValue($structure, 'name');
95+
$content = $this->getPartByPartNumber($structure, 1);
96+
$returnParts[$mimeType][] = new Part($name, $content);
9397
}
9498

9599
}
96100

101+
private function getParameterValue($structure, $parameterName)
102+
{
103+
if (!isset($structure->parameters) || !is_array($structure->parameters)) {
104+
return null;
105+
}
106+
107+
foreach($structure->parameters as $parameter) {
108+
if (strtolower($parameter->attribute) === strtolower($parameterName)) {
109+
return $parameter->value;
110+
}
111+
}
112+
113+
return null;
114+
}
115+
97116
/**
98117
* @param $structure
99118
* @param $partToGet

src/Part.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DivineOmega\EmailStructureParser;
4+
5+
class Part
6+
{
7+
public $name;
8+
public $content;
9+
10+
public function __construct($name, $content)
11+
{
12+
$this->name = $name;
13+
$this->content = $content;
14+
}
15+
}

src/example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@
2121
var_dump($parts['TEXT/HTML']);
2222

2323
// Save attached PNG images
24-
foreach($parts['IMAGE/PNG'] as $key => $image) {
25-
file_put_contents($key.'.png', $image);
24+
foreach($parts['IMAGE/PNG'] as $image) {
25+
file_put_contents($image->name, $image->content);
2626
}

0 commit comments

Comments
 (0)