Skip to content
6 changes: 4 additions & 2 deletions src/Fetch/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ public function __construct(Message $message, $structure, $partIdentifier = null
} elseif (isset($parameters['name'])) {
$this->setFileName($parameters['name']);
}

$this->size = $structure->bytes;

if (isset($structure->bytes)) {
$this->size = $structure->bytes;
}

$this->mimeType = Message::typeIdToString($structure->type);

Expand Down
8 changes: 5 additions & 3 deletions src/Fetch/MIME.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public static function decode($text, $targetCharset = 'utf-8')

$result = '';

foreach (imap_mime_header_decode($text) as $word) {
$ch = 'default' === $word->charset ? 'ascii' : $word->charset;
$encoding = mb_detect_encoding($text, mb_detect_order(), false);

$result .= iconv($ch, $targetCharset, $word->text);
if($encoding == "UTF-8") {
$text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
}

$result = iconv(mb_detect_encoding($text, mb_detect_order(), false), "UTF-8//IGNORE", $text);

return $result;
}
}
24 changes: 18 additions & 6 deletions src/Fetch/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ class Message
*/
public static $charsetFlag = '//TRANSLIT';

/**
* This value defines the flag set for encoding for iconv to ignore the
* iconv(): Detected an illegal character in input string.
*
* @var string
*/
public static $charsetAltFlag = '//IGNORE';

/**
* These constants can be used to easily access available flags
*/
Expand Down Expand Up @@ -230,12 +238,15 @@ protected function loadMessage()
/* First load the message overview information */

if(!is_object($messageOverview = $this->getOverview()))

return false;

$this->subject = MIME::decode($messageOverview->subject, self::$charset);
$this->date = strtotime($messageOverview->date);
$this->size = $messageOverview->size;
$subject = property_exists($messageOverview, 'subject')? $messageOverview->subject : '';
$date = property_exists($messageOverview, 'date')? $messageOverview->date : '';
$size = property_exists($messageOverview, 'size')? $messageOverview->size : '';

$this->subject = MIME::decode($subject, self::$charset . self::$charsetAltFlag);
$this->date = strtotime($date);
$this->size = $size;

foreach (self::$flagTypes as $flag)
$this->status[$flag] = ($messageOverview->$flag == 1);
Expand Down Expand Up @@ -672,9 +683,10 @@ protected function processAddressObject($addresses)
foreach ($addresses as $address) {
if (property_exists($address, 'mailbox') && $address->mailbox != 'undisclosed-recipients') {
$currentAddress = array();
$currentAddress['address'] = $address->mailbox . '@' . $address->host;
$host = property_exists($address, 'host')?$address->host:'';
$currentAddress['address'] = $address->mailbox . '@' . $host;
if (isset($address->personal)) {
$currentAddress['name'] = MIME::decode($address->personal, self::$charset);
$currentAddress['name'] = MIME::decode($address->personal, self::$charset . self::$charsetAltFlag);
}
$outputAddresses[] = $currentAddress;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Fetch/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ public function getMessageByUid($uid)
}
}

/**
* This function returns all the imap errors to prevent the following
* warning from imap_close and imap_expunge
* 'Unknown: Warning: MIME header encountered in non-MIME message (errflg=3)'
*/
public function getImapErrors()
{
return imap_errors();
}

/**
* This function removes all of the messages flagged for deletion from the mailbox.
*
Expand Down