Skip to content

Commit 49be725

Browse files
authored
Merge pull request #1 from SteJaySulli/feature/part-content-id
Added code to expose part content ID
2 parents cd5522b + f613bed commit 49be725

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,18 @@ foreach($parts['IMAGE/PNG'] as $image) {
4747
file_put_contents($image->name, $image->content);
4848
}
4949
```
50+
51+
## Content IDs
52+
53+
Some emails embed images into the HTML content using `cid:` urls. These URLs link to the content ID of another part within the email, rather than being an absolute `https://` URL that can be resolved normally.
54+
55+
If one exists, this content ID will be exposed via the `contentId` property of the `Part`:
56+
57+
```php
58+
foreach($parts['IMAGE/PNG'] as $image) {
59+
// Store the file as in the above example:
60+
file_put_contents($image->name, $image->content);
61+
// You would then want to store this relationship in your database:
62+
echo "{$image->contentId} => {$image->name}\n";
63+
}
64+
```

src/EmailStructureParser.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,19 @@ private function getPart(&$returnParts, $structure, $partNumber = false)
8383
{
8484
$mimeType = $this->getMimeType($substructure);
8585
$name = $this->getParameterValue($substructure, 'name');
86+
$contentId = $this->getPartContentId($substructure);
8687
$content = $this->getPartByPartNumber($substructure, $partToGet);
87-
$returnParts[$mimeType][] = new Part($name, $content);
88+
$returnParts[$mimeType][] = new Part($name, $content, $contentId);
8889
}
8990
}
9091
}
9192
else
9293
{
9394
$mimeType = $this->getMimeType($structure);
9495
$name = $this->getParameterValue($structure, 'name');
96+
$contentId = $this->getPartContentId($structure);
9597
$content = $this->getPartByPartNumber($structure, 1);
96-
$returnParts[$mimeType][] = new Part($name, $content);
98+
$returnParts[$mimeType][] = new Part($name, $content, $contentId);
9799
}
98100

99101
}
@@ -137,6 +139,18 @@ private function getPartByPartNumber($structure, $partToGet)
137139
}
138140
}
139141

142+
/**
143+
* @param $structure
144+
* @return string
145+
*/
146+
private function getPartContentId($structure) {
147+
if(isset($structure->id)) {
148+
// Content IDs are wrapped in <>, so we remove these:
149+
return preg_replace('/[<>]/', '', $structure->id);
150+
}
151+
return null;
152+
}
153+
140154
/**
141155
* @param $structure
142156
* @return string

src/Part.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ class Part
77
public $name;
88
public $content;
99

10-
public function __construct($name, $content)
10+
public function __construct($name, $content, $contentId = null)
1111
{
1212
$this->name = $name;
1313
$this->content = $content;
14+
$this->contentId = $contentId;
1415
}
1516
}

0 commit comments

Comments
 (0)