Skip to content

Commit 881ae21

Browse files
committed
Text/Html body fetched as attachment if subtype is null Webklex#34
1 parent decd810 commit 881ae21

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
66

77
## [UNRELEASED]
88
### Fixed
9-
- NaN
9+
- Text/Html body fetched as attachment if subtype is null #34
1010

1111
### Added
1212
- NaN
1313

1414
### Affected Classes
15-
- NaN
15+
- [Message::class](src/Message.php)
16+
- [Part::class](src/Part.php)
1617

1718
### Breaking changes
1819
- NaN

src/Message.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -438,34 +438,32 @@ private function fetchStructure($structure) {
438438
*/
439439
private function fetchPart(Part $part) {
440440

441-
if ($part->type == IMAP::MESSAGE_TYPE_TEXT && ($part->ifdisposition == 0 || (empty($part->disposition) || !in_array(strtolower($part->disposition), ['attachment', 'inline'])) ) ) {
442-
443-
if ( in_array(($subtype = strtolower($part->subtype)), ["plain", "csv", "html"]) && $part->filename == null && $part->name == null) {
444-
$encoding = $this->getEncoding($part);
445-
446-
$content = $this->decodeString($part->content, $part->encoding);
447-
448-
// We don't need to do convertEncoding() if charset is ASCII (us-ascii):
449-
// ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
450-
// https://stackoverflow.com/a/11303410
451-
//
452-
// us-ascii is the same as ASCII:
453-
// ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
454-
// prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
455-
// based on the typographical symbols predominantly in use there.
456-
// https://en.wikipedia.org/wiki/ASCII
457-
//
458-
// convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
459-
if ($encoding != 'us-ascii') {
460-
$content = $this->convertEncoding($content, $encoding);
461-
}
462-
463-
$this->bodies[$subtype == "plain" ? "text" : $subtype] = $content;
464-
} else {
465-
$this->fetchAttachment($part);
466-
}
467-
} else {
441+
if ($part->isAttachment()) {
468442
$this->fetchAttachment($part);
443+
}else{
444+
$encoding = $this->getEncoding($part);
445+
446+
$content = $this->decodeString($part->content, $part->encoding);
447+
448+
// We don't need to do convertEncoding() if charset is ASCII (us-ascii):
449+
// ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
450+
// https://stackoverflow.com/a/11303410
451+
//
452+
// us-ascii is the same as ASCII:
453+
// ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
454+
// prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
455+
// based on the typographical symbols predominantly in use there.
456+
// https://en.wikipedia.org/wiki/ASCII
457+
//
458+
// convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
459+
if ($encoding != 'us-ascii') {
460+
$content = $this->convertEncoding($content, $encoding);
461+
}
462+
463+
$subtype = strtolower($part->subtype);
464+
$subtype = $subtype == "plain" || $subtype == "" ? "text" : $subtype;
465+
466+
$this->bodies[$subtype] = $content;
469467
}
470468
}
471469

src/Part.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,18 @@ private function parseEncoding(){
293293
}
294294
}
295295

296+
/**
297+
* Check if the current part represents an attachment
298+
*
299+
* @return bool
300+
*/
301+
public function isAttachment(){
302+
if ($this->type == IMAP::MESSAGE_TYPE_TEXT && ($this->ifdisposition == 0 || (empty($this->disposition) || !in_array(strtolower($this->disposition), ['attachment', 'inline'])) ) ) {
303+
if (($this->subtype == null || in_array((strtolower($this->subtype)), ["plain", "csv", "html"])) && $this->filename == null && $this->name == null) {
304+
return false;
305+
}
306+
}
307+
return true;
308+
}
309+
296310
}

0 commit comments

Comments
 (0)