Skip to content

Commit 0c239ee

Browse files
committed
WIP/dirty/needs polishing/hack: added support for listing/receiving any mime part of message (can be used e.g. for parsing DSNs)
1 parent 0dc7a90 commit 0c239ee

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

MailLibrary/Mail.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ protected function initializeStructure()
257257
$this->structure = $this->connection->getDriver()->getStructure($this->id, $this->mailbox);
258258
}
259259

260+
/** @internal */
261+
public function getStructure(): IStructure {
262+
$this->structure !== NULL || $this->initializeStructure();
263+
return $this->structure;
264+
}
265+
260266
protected function initializeFlags()
261267
{
262268
$this->connection->getDriver()->switchMailbox($this->mailbox->getName());

MailLibrary/MimePart.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* This file is part of the imap-mail-list-extractor.
4+
* Copyright (c) 2017 Grifart spol. s r.o. (https://grifart.cz)
5+
*/
6+
7+
namespace greeny\MailLibrary;
8+
9+
use greeny\MailLibrary\Drivers\IDriver;
10+
11+
class MimePart
12+
{
13+
/** @var string */
14+
private $partId;
15+
16+
/** @var string */
17+
private $mimeType;
18+
19+
/** @var string */
20+
private $name;
21+
22+
/** @var string */
23+
private $encoding;
24+
25+
/** @var IDriver */
26+
private $driver;
27+
28+
/** @var string */
29+
private $mailId;
30+
31+
public function __construct(IDriver $driver, $mailId, $partId, $mimeType, $name, $encoding)
32+
{
33+
$this->partId = $partId;
34+
$this->mailId = $mailId;
35+
$this->mimeType = $mimeType;
36+
$this->name = $name;
37+
$this->encoding = $encoding;
38+
$this->driver = $driver;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getPartId(): string
45+
{
46+
return $this->partId;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getMimeType(): string
53+
{
54+
return $this->mimeType;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getName(): string
61+
{
62+
return $this->name;
63+
}
64+
65+
/**
66+
* @return string
67+
*/
68+
public function getEncoding(): string
69+
{
70+
return $this->encoding;
71+
}
72+
73+
public function getContent()
74+
{
75+
// todo: this automatically decodes content type if supported, support for getting RAW content?
76+
return $this->driver->getBody($this->mailId, [[
77+
'id' => $this->partId,
78+
'encoding' => $this->encoding,
79+
]]);
80+
}
81+
82+
}

MailLibrary/Structures/IStructure.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace greeny\MailLibrary\Structures;
77

88
use greeny\MailLibrary\Attachment;
9+
use greeny\MailLibrary\MimePart;
910

1011
interface IStructure {
1112
/**
@@ -27,4 +28,7 @@ function getTextBody();
2728
* @return Attachment[]
2829
*/
2930
function getAttachments();
31+
32+
/** @return MimePart[] */
33+
public function getParts(): array;
3034
}

MailLibrary/Structures/ImapStructure.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use greeny\MailLibrary\Attachment;
99
use greeny\MailLibrary\Drivers\ImapDriver;
1010
use greeny\MailLibrary\Mailbox;
11+
use greeny\MailLibrary\MimePart;
1112

1213
class ImapStructure implements IStructure {
1314
const TYPE_TEXT = 0;
@@ -37,6 +38,8 @@ class ImapStructure implements IStructure {
3738
self::TYPE_OTHER => 'other',
3839
);
3940

41+
private $messageParts = [];
42+
4043
/** @var \greeny\MailLibrary\Drivers\ImapDriver */
4144
protected $driver;
4245

@@ -64,6 +67,9 @@ class ImapStructure implements IStructure {
6467
/** @var Mailbox */
6568
protected $mailbox;
6669

70+
/** @var array */
71+
private $rawStructure = [];
72+
6773
/**
6874
* @param ImapDriver $driver
6975
* @param object $structure
@@ -72,6 +78,7 @@ class ImapStructure implements IStructure {
7278
*/
7379
public function __construct(ImapDriver $driver, $structure, $mailId, Mailbox $mailbox)
7480
{
81+
$this->rawStructure = $structure;
7582
$this->driver = $driver;
7683
$this->id = $mailId;
7784
$this->mailbox = $mailbox;
@@ -84,6 +91,14 @@ public function __construct(ImapDriver $driver, $structure, $mailId, Mailbox $ma
8491
}
8592
}
8693

94+
/**
95+
* @return array
96+
*/
97+
public function getRawStructure()
98+
{
99+
return $this->rawStructure;
100+
}
101+
87102
/**
88103
* @return string
89104
*/
@@ -133,6 +148,13 @@ public function getAttachments()
133148
return $this->attachments;
134149
}
135150

151+
/** @return MimePart[] */
152+
public function getParts(): array
153+
{
154+
$this->driver->switchMailbox($this->mailbox->getName());
155+
return $this->messageParts;
156+
}
157+
136158
protected function addStructurePart($structure, $partId)
137159
{
138160
$type = $structure->type;
@@ -151,13 +173,24 @@ protected function addStructurePart($structure, $partId)
151173
}
152174
}
153175

176+
$this->messageParts[] = new MimePart(
177+
$this->driver,
178+
$this->id,
179+
$partId,
180+
self::$typeTable[$type]. '/' . $subtype,
181+
$parameters['filename'] ?? $parameters['name'] ?? '',
182+
$encoding
183+
);
184+
185+
154186
if(isset($parameters['filename']) || isset($parameters['name'])) {
155187
$this->attachmentsIds[] = array(
156188
'id' => $partId,
157189
'encoding' => $encoding,
158190
'name' => isset($parameters['filename']) ? $parameters['filename'] : $parameters['name'],
159191
'type' => self::$typeTable[$type]. '/' . $subtype,
160192
);
193+
161194
} else if($type === self::TYPE_TEXT) {
162195
if($subtype === 'HTML') {
163196
$this->htmlBodyIds[] = array('id' => $partId, 'encoding' => $encoding);

MailLibrary/loader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'greeny\maillibrary\contactlist' => 'ContactList.php',
1515
'greeny\maillibrary\contact' => 'Contact.php',
1616
'greeny\maillibrary\attachment' => 'Attachment.php',
17+
'greeny\maillibrary\mimepart' => 'MimePart.php',
1718
'greeny\maillibrary\structures\istructure' => 'Structures/IStructure.php',
1819
'greeny\maillibrary\structures\imapstructure' => 'Structures/ImapStructure.php',
1920
'greeny\maillibrary\drivers\idriver' => 'Drivers/IDriver.php',

0 commit comments

Comments
 (0)