Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Fix mail messages with blank line on top #24

Merged
merged 3 commits into from
Sep 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Storage/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public function __construct(array $params)
} else {
$params['raw'] = stream_get_contents($params['file']);
}

$params['raw'] = ltrim($params['raw']);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should apply the workaround always? (out of the if block)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense.

}

if (!empty($params['flags'])) {
Expand Down
54 changes: 23 additions & 31 deletions test/Storage/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,19 @@ public function testInvalidFile()
$this->fail('no exception raised while loading unknown file');
}

public function testIsMultipart()
{
$message = new Message(['file' => $this->_file]);

$this->assertTrue($message->isMultipart());
}

public function testGetHeader()
{
$message = new Message(['file' => $this->_file]);

$this->assertEquals($message->subject, 'multipart');
}

public function testGetDecodedHeader()
{
$message = new Message(['file' => $this->_file]);

$this->assertEquals('Peter Müller <peter-mueller@example.com>', $message->from);
}

public function testGetHeaderAsArray()
/**
* @dataProvider filesProvider
*/
public function testParseFile($params)
{
$message = new Message(['file' => $this->_file]);
$message = new Message($params);

$this->assertEquals($message->getHeader('subject', 'array'), ['multipart']);
$this->assertTrue($message->isMultipart(), 'isMultipart() value not match');
$this->assertEquals($message->subject, 'multipart', 'subject value not match');
$this->assertEquals('Peter Müller <peter-mueller@example.com>', $message->from, 'from value not match');
$this->assertEquals(['multipart'], $message->getHeader('subject', 'array'), 'getHeader() value not match');
}

public function testGetHeaderFromOpenFile()
{
$fh = fopen($this->_file, 'r');
$message = new Message(['file' => $fh]);

$this->assertEquals($message->subject, 'multipart');
}

public function testGetFirstPart()
{
Expand Down Expand Up @@ -428,4 +406,18 @@ public function testStrictParseMessage()
$raw = "From foo@example.com Sun Jan 01 00:00:00 2000\n" . $raw;
$message = new Message(['raw' => $raw, 'strict' => true]);
}

public function filesProvider()
{
$filePath = __DIR__ . '/../_files/mail.txt';
$fileBlankLineOnTop = __DIR__ . '/../_files/mail_blank_top_line.txt';

return [
// Description => [params]
'resource' => [['file' => fopen($filePath, 'r')]],
'file path' => [['file' => $filePath]],
'raw' => [['raw' => file_get_contents($filePath)]],
'file with blank line on top' => [['file' => $fileBlankLineOnTop]],
];
}
}
28 changes: 28 additions & 0 deletions test/_files/mail_blank_top_line.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

To: foo@example.com
Subject: multipart
Date: Sun, 01 Jan 2000 00:00:00 +0000
From: =?UTF-8?Q?"Peter M=C3=BCller"?= <peter-mueller@example.com>
ContENT-type: multipart/alternative; boUNDary="crazy-multipart"
Message-ID: <CALTvGe4_oYgf9WsYgauv7qXh2-6=KbPLExmJNG7fCs9B=1nOYg@mail.example.com>
MIME-version: 1.0

multipart message
--crazy-multipart
Content-type: text/plain

The first part
is horizontal

--crazy-multipart
Content-type: text/x-vertical

T s p i v
h e a s e
e c r r
o t t
n i
d c
a
l
--crazy-multipart--