Skip to content

Commit

Permalink
Fix #3
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Nov 22, 2013
1 parent a3c7056 commit 7faf652
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/Ddeboer/Imap/Message/Headers.php
Expand Up @@ -15,9 +15,11 @@ public function __construct(\stdClass $headers)
if (isset($headers->subject)) {
$subject = '';
foreach (\imap_mime_header_decode($headers->subject) as $part) {
$subject .= $part->text;
// $part->charset can also be 'default', i.e. plain US-ASCII
$charset = $part->charset == 'default' ? 'auto' : $part->charset;
$subject .= \mb_convert_encoding($part->text, 'UTF-8', $charset);
}
$this->array['subject'] = mb_convert_encoding($subject, 'UTF-8');
$this->array['subject'] = $subject;
}

$this->array['msgno'] = (int) $this->array['msgno'];
Expand Down
6 changes: 4 additions & 2 deletions src/Ddeboer/Imap/Message/Part.php
Expand Up @@ -169,7 +169,9 @@ public function getDecodedContent()
// If this part is a text part, try to convert its encoding to UTF-8.
// We don't want to convert an attachment's encoding.
if ($this->getType() === self::TYPE_TEXT
&& strtolower($this->getCharset()) != 'utf-8') {
&& null !== $this->getCharset()
&& strtolower($this->getCharset()) != 'utf-8'
) {
$this->decodedContent = \mb_convert_encoding(
$this->decodedContent,
'UTF-8'
Expand Down Expand Up @@ -304,7 +306,7 @@ public function getDisposition()
*/
protected function doGetContent($keepUnseen = false)
{
$this->content = \imap_fetchbody(
return \imap_fetchbody(
$this->stream,
$this->messageNumber,
$this->partNumber ?: 1,
Expand Down
8 changes: 4 additions & 4 deletions tests/Ddeboer/Imap/Tests/AbstractTest.php
Expand Up @@ -64,16 +64,16 @@ protected function createMailbox($name)

protected function createTestMessage(
Mailbox $mailbox,
$subject = 'Don\t panic!',
$subject = 'Don\'t panic!',
$contents = 'Don\'t forget your towel',
$from = 'someone@there.com',
$to = 'me@here.com',
$contents = 'Don\'t forget your towel'
$to = 'me@here.com'
) {
$message = "From: $from\r\n"
. "To: $to\r\n"
. "Subject: $subject\r\n"
. "\r\n"
. "$contents\r\n";
. "$contents";

$mailbox->addMessage($message);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Ddeboer/Imap/Tests/MessageTest.php
Expand Up @@ -15,6 +15,7 @@ public function setUp()
$this->createTestMessage($this->mailbox, 'Message A');
$this->createTestMessage($this->mailbox, 'Message B');
$this->createTestMessage($this->mailbox, 'Message C');
$this->createTestMessage($this->mailbox, 'lietuviškos raidės', 'lietuviškos raidės');
}

public function testKeepUnseen()
Expand All @@ -31,4 +32,11 @@ public function testKeepUnseen()
$message->keepUnseen()->getBodyText();
$this->assertFalse($message->isSeen());
}

public function testSubjectEncoding()
{
$message = $this->mailbox->getMessage(4);
$this->assertEquals('lietuviškos raidės', $message->getSubject());
$this->assertEquals('lietuviškos raidės', $message->getBodyText());
}
}

0 comments on commit 7faf652

Please sign in to comment.