Skip to content

Commit 146caf5

Browse files
committed
Problem regarding attachments and body content solved
1 parent 22ea856 commit 146caf5

File tree

1 file changed

+69
-57
lines changed

1 file changed

+69
-57
lines changed

src/IMAP/Message.php

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,10 @@ private function parseBody() {
311311
* Fetch the Message structure
312312
*
313313
* @param $structure
314-
*
315314
* @param mixed $partNumber
316315
*/
317316
private function fetchStructure($structure, $partNumber = null) {
318-
if ($structure->type == self::TYPE_TEXT && $partNumber == null) {
317+
if ($structure->type == self::TYPE_TEXT) {
319318
if ($structure->subtype == "PLAIN") {
320319
if (!$partNumber) {
321320
$partNumber = 1;
@@ -333,6 +332,8 @@ private function fetchStructure($structure, $partNumber = null) {
333332

334333
$this->bodies['text'] = $body;
335334

335+
$this->fetchAttachment($structure, $partNumber);
336+
336337
} elseif ($structure->subtype == "HTML") {
337338
if (!$partNumber) {
338339
$partNumber = 1;
@@ -359,72 +360,83 @@ private function fetchStructure($structure, $partNumber = null) {
359360
$this->fetchStructure($subStruct, $prefix . ($index + 1));
360361
}
361362
} else {
362-
switch ($structure->type) {
363-
case self::TYPE_APPLICATION:
364-
$type = 'application';
365-
break;
366-
case self::TYPE_AUDIO:
367-
$type = 'audio';
368-
break;
369-
case self::TYPE_IMAGE:
370-
$type = 'image';
371-
break;
372-
case self::TYPE_VIDEO:
373-
$type = 'video';
374-
break;
375-
case self::TYPE_MODEL:
376-
$type = 'model';
377-
break;
378-
case self::TYPE_OTHER:
379-
$type = 'other';
380-
break;
381-
default:
382-
$type = 'other';
383-
break;
384-
}
363+
$this->fetchAttachment($structure, $partNumber);
364+
}
365+
}
385366

386-
$content = imap_fetchbody($this->client->connection, $this->uid, ($partNumber) ? $partNumber : 1, $this->fetch_options);
387367

388-
$attachment = new \stdClass;
389-
$attachment->type = $type;
390-
$attachment->content_type = $type.'/'.strtolower($structure->subtype);
391-
$attachment->content = $this->decodeString($content, $structure->encoding);
368+
/**
369+
* Fetch the Message attachment
370+
*
371+
* @param object $structure
372+
* @param mixed $partNumber
373+
*/
374+
protected function fetchAttachment($structure, $partNumber){
375+
switch ($structure->type) {
376+
case self::TYPE_APPLICATION:
377+
$type = 'application';
378+
break;
379+
case self::TYPE_AUDIO:
380+
$type = 'audio';
381+
break;
382+
case self::TYPE_IMAGE:
383+
$type = 'image';
384+
break;
385+
case self::TYPE_VIDEO:
386+
$type = 'video';
387+
break;
388+
case self::TYPE_MODEL:
389+
$type = 'model';
390+
break;
391+
case self::TYPE_OTHER:
392+
$type = 'other';
393+
break;
394+
default:
395+
$type = 'other';
396+
break;
397+
}
392398

393-
$attachment->id = false;
394-
if (property_exists($structure, 'id')) {
395-
$attachment->id = str_replace(['<', '>'], '', $structure->id);
396-
}
399+
$content = imap_fetchbody($this->client->connection, $this->uid, ($partNumber) ? $partNumber : 1, $this->fetch_options);
400+
401+
$attachment = new \stdClass;
402+
$attachment->type = $type;
403+
$attachment->content_type = $type.'/'.strtolower($structure->subtype);
404+
$attachment->content = $this->decodeString($content, $structure->encoding);
405+
406+
$attachment->id = false;
407+
if (property_exists($structure, 'id')) {
408+
$attachment->id = str_replace(['<', '>'], '', $structure->id);
409+
}
397410

398-
$attachment->name = false;
399-
if (property_exists($structure, 'dparameters')) {
400-
foreach ($structure->dparameters as $parameter) {
401-
if ($parameter->attribute == "filename") {
402-
$attachment->name = $parameter->value;
403-
break;
404-
}
411+
$attachment->name = false;
412+
if (property_exists($structure, 'dparameters')) {
413+
foreach ($structure->dparameters as $parameter) {
414+
if ($parameter->attribute == "filename") {
415+
$attachment->name = $parameter->value;
416+
break;
405417
}
406418
}
419+
}
407420

408-
if (!$attachment->name && property_exists($structure, 'parameters')) {
409-
foreach ($structure->parameters as $parameter) {
410-
if ($parameter->attribute == "name") {
411-
$attachment->name = $parameter->value;
412-
break;
413-
}
421+
if (!$attachment->name && property_exists($structure, 'parameters')) {
422+
foreach ($structure->parameters as $parameter) {
423+
if ($parameter->attribute == "name") {
424+
$attachment->name = $parameter->value;
425+
break;
414426
}
415427
}
428+
}
416429

417-
if ($attachment->type == 'image') {
418-
$attachment->img_src = 'data:'.$attachment->content_type.';base64,'.base64_encode($attachment->content);
419-
}
430+
if ($attachment->type == 'image') {
431+
$attachment->img_src = 'data:'.$attachment->content_type.';base64,'.base64_encode($attachment->content);
432+
}
420433

421-
if(property_exists($attachment, 'name')){
422-
if($attachment->name != false){
423-
if ($attachment->id) {
424-
$this->attachments[$attachment->id] = $attachment;
425-
} else {
426-
$this->attachments[] = $attachment;
427-
}
434+
if(property_exists($attachment, 'name')){
435+
if($attachment->name != false){
436+
if ($attachment->id) {
437+
$this->attachments[$attachment->id] = $attachment;
438+
} else {
439+
$this->attachments[] = $attachment;
428440
}
429441
}
430442
}

0 commit comments

Comments
 (0)