Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing boundaries #5

Merged
merged 10 commits into from Feb 22, 2017
10 changes: 4 additions & 6 deletions PlancakeEmailParser.php
Expand Up @@ -411,17 +411,15 @@ public function getBody($returnType = self::PLAINTEXT)
$contentTypeRegex = '/^Content-Type: ?text\/plain/i';
}

// there could be more than one boundary
preg_match_all('!boundary=(.*?)[;$]!mi', $this->emailRawContent, $matches);
// there could be more than one boundary. This also skips the quotes if they are included.
preg_match_all('/boundary=(?:|")([a-zA-Z0-9\(\)_\/+-]+)(?:|")(?:$|;)/mi', $this->emailRawContent, $matches);
$boundariesRaw = $matches[1];
$boundaries = array();
foreach ($boundariesRaw as $i => $v) {
// sometimes boundaries are delimited by quotes - we want to remove them
$tempboundary = str_replace(array("'", '"'), '', $v);
// actual boundary lines start with --
$boundaries[] = '--' . $tempboundary;
$boundaries[] = '--' . $v;
// or start and end with --
$boundaries[] = '--' . $tempboundary . '--';
$boundaries[] = '--' . $v . '--';
}

foreach ($this->rawBodyLines as $line) {
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -20,7 +20,9 @@
"ext-mbstring": "*"
},
"require-dev": {
"squizlabs/php_codesniffer": "~2.5.1"
"squizlabs/php_codesniffer": "~2.5.1",
"phpunit/phpunit": "^6.0",
"symfony/yaml": "^3.2"
},
"autoload": {
"psr-0": { "PlancakeEmailParser": "." }
Expand Down
112 changes: 112 additions & 0 deletions tests/PlancakeEmailParserTest.php
@@ -0,0 +1,112 @@
<?php
use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;

class PlancakeEmailParserTest extends TestCase
{

public function testSubject()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['subject'], $email->getSubject());
}
}

public function testFrom()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['from'], $email->getFrom());
}
}

public function testTo()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['to'], $email->getTo());
}
}

public function testCc()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['cc'], $email->getCc());
}
}

public function testBcc()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['bcc'], $email->getBcc());
}
}

public function testSender()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['sender'], $email->getSender());
}
}

public function testPlainBody()
{
foreach (glob(__DIR__ . '/emails/*.txt') as $testFile) {

$answerFile = str_replace('.txt', '.yml', $testFile);
$answers = Yaml::parse(
file_get_contents($answerFile)
);

$email = new PlancakeEmailParser(file_get_contents($testFile));

$this->assertEquals($answers['plainbody'], $email->getPlainBody());
}
}
}
2 changes: 2 additions & 0 deletions tests/bootstrap.php
@@ -0,0 +1,2 @@
<?php
require_once(dirname(__DIR__) . '/vendor/autoload.php');
16 changes: 16 additions & 0 deletions tests/emails/0.yml
@@ -0,0 +1,16 @@
subject: 'zażółć gęślą jaźń i zrób próbne zadanie %%tod'
from:
- name: Pawel
email: pawel@test.com
to:
- name: ''
email: dan@test.com
cc: []
bcc: []
sender: []
plainbody: |-
This is a nonesense test-email to check Plancake Mail to Inbox feature.
All Polish diacritics included ;-) tag also included for additional tests.
It should look like in the picture attached.
Best regards
Pawel
11 changes: 11 additions & 0 deletions tests/emails/1.yml
@@ -0,0 +1,11 @@
subject: this is a test
from:
- name: Dan @ Test.com
email: dan@test.com
to:
- name: 'Daniele '
email: danielet@test.com
cc: []
bcc: []
sender: []
plainbody: Hope it works!
11 changes: 11 additions & 0 deletions tests/emails/2.yml
@@ -0,0 +1,11 @@
subject: 'これはテストです'
from:
- name: Dan
email: dan@test.com
to:
- name: 'Daniele'
email: daniele@test.com
cc: []
bcc: []
sender: []
plainbody: 'それは作品を期待'
11 changes: 11 additions & 0 deletions tests/emails/from_ipad_with_attachment.yml
@@ -0,0 +1,11 @@
subject: Voice Memo
from:
- name: Dan Occhi
email: dan@example.com
to:
- name: Inbox_danocch.it_2063@examplebox.com
email: Inbox_danocch.it_2063@examplebox.com
cc: []
bcc: []
sender: []
plainbody: ''
18 changes: 18 additions & 0 deletions tests/emails/subject_on_multiple_lines.yml
@@ -0,0 +1,18 @@
subject: 'Re: Testo Del di Soggetto Che Va A Capo In UTF8 '
from:
- name: ''
email: info@domain.it
to:
- name: ''
email: bounce@domain-bounce.it
cc: []
bcc: []
sender: []
plainbody: |-



Da: info@domain.it
Oggetto: Grazie!!!

Nome Cognome