Skip to content

Commit cd46556

Browse files
committed
tests/updates to better hanlde foreign characters
1 parent 8b2b8b1 commit cd46556

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

decode/MailParser.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public function getHtml()
356356

357357
/**
358358
*
359-
* @return array
359+
* @return string
360360
* @throws \Exception if a to header is not found or if there are no recipient
361361
*/
362362
public function getTo()
@@ -365,7 +365,8 @@ public function getTo()
365365
throw new \Exception("Couldn't find the recipients of the email");
366366
}
367367

368-
return \utf8_encode(\iconv_mime_decode($this->rawFields['to']));
368+
// @see https://www.php.net/manual/en/function.imap-utf8.php#102081
369+
return \iconv_mime_decode($this->rawFields['to'], 0, "UTF-8");
369370
}
370371

371372
/**
@@ -379,7 +380,8 @@ public function getSubject()
379380
throw new \Exception("Couldn't find the subject of the email");
380381
}
381382

382-
return \utf8_encode(\iconv_mime_decode($this->rawFields['subject']));
383+
// @see https://www.php.net/manual/en/function.imap-utf8.php#102081
384+
return \iconv_mime_decode($this->rawFields['subject'], 0, "UTF-8");
383385
}
384386

385387
/**
@@ -457,8 +459,9 @@ public function getFrom()
457459
if (!isset($this->rawFields['from'])) {
458460
throw new \Exception("Couldn't find the sender of the email");
459461
}
460-
461-
return \utf8_encode(\iconv_mime_decode($this->rawFields['from']));
462+
463+
// @see https://www.php.net/manual/en/function.imap-utf8.php#102081
464+
return \iconv_mime_decode($this->rawFields['from'], 0, "UTF-8");
462465
}
463466

464467
/**

tests/MailParserTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ public function testSubject()
1515
$this->assertEquals('foobar.com download complete', $email->getSubject());
1616
}
1717

18+
public function testSubjectForeign()
19+
{
20+
$file = __DIR__ . '/emails/m0009';
21+
$email = new MailParser(\file_get_contents($file));
22+
23+
$this->assertEquals("これはテストです", $email->getSubject());
24+
$this->assertEquals("それは作品を期待", $email->getPlain());
25+
}
26+
1827
public function testFrom()
1928
{
2029
$file = __DIR__ . '/emails/m0001';
2130
$email = new MailParser(\file_get_contents($file));
2231

2332
$this->assertEquals('Name <name@company.com>', $email->getFrom());
33+
$this->assertEquals("Mail avec fichier attaché de 1ko", $email->getSubject());
2434
}
2535

2636
public function testTo()

tests/emails/m0009

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Delivered-To: daniele@test.com
2+
Received: by 10.114.17.33 with SMTP id l1csp4056ldd;
3+
Tue, 18 Sep 2012 03:26:33 -0700 (PDT)
4+
Received: by 10.221.1.137 with SMTP id nq9mr9146920vcb.16.1347963992703;
5+
Tue, 18 Sep 2012 03:26:32 -0700 (PDT)
6+
Return-Path: <dan@test.com>
7+
Received: from mail-vb0-f46.google.com (mail-vb0-f46.google.com [209.85.212.46])
8+
by mx.google.com with ESMTPS id nr9si7739478vcb.47.2012.09.18.03.26.31
9+
(version=TLSv1/SSLv3 cipher=OTHER);
10+
Tue, 18 Sep 2012 03:26:32 -0700 (PDT)
11+
Received-SPF: neutral (google.com: 209.85.212.46 is neither permitted nor denied by best guess record for domain of dan@test.com) client-ip=209.85.212.46;
12+
Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.46 is neither permitted nor denied by best guess record for domain of dan@test.com) smtp.mail=dan@test.com
13+
Received: by mail-vb0-f46.google.com with SMTP id ff1so4097773vbb.19
14+
for <daniele@test.com>; Tue, 18 Sep 2012 03:26:31 -0700 (PDT)
15+
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
16+
d=google.com; s=20120113;
17+
h=mime-version:x-originating-ip:from:date:message-id:subject:to
18+
:content-type:x-gm-message-state;
19+
bh=AkxZ77/pitG59A/NWKlbLstrhunK6KXfB2fNB4YSalk=;
20+
b=bhfvZKO9bbkr4akjdJ5vaot480TiITsjBMdjtxCMAg4RPnoed6IsivejP9cJZcIIPq
21+
N0O2CaPv1z9DyT4DXlPYJTAMUSvHg8KWZIUp7rfKZ0X7Y9OWWIHkIaApqfMpM61OF0qo
22+
wbCG7boJ2fbtdDPZQ3z+LamDKAChU7ayuplQYN0vMqkpRlYw9YLnM6BOO4Yj66TZ8q6M
23+
WAVdM1kFVP+r2QGd7ka3UWM55Ah9R9dg2SRmBN3W6EBV91IVWw4DQ9Ufucr65YnCU/GA
24+
qe3AuaeHf0Th2CnMn/mlMbeEm+vG3WD+G3ZQ8+UpKkDFdN4GlrB3JnnDc6AV3o4weske
25+
tYQA==
26+
Received: by 10.58.65.40 with SMTP id u8mr9411987ves.47.1347963991825; Tue, 18
27+
Sep 2012 03:26:31 -0700 (PDT)
28+
MIME-Version: 1.0
29+
Received: by 10.58.222.103 with HTTP; Tue, 18 Sep 2012 03:26:11 -0700 (PDT)
30+
X-Originating-IP: [109.158.215.220]
31+
From: "Dan" <dan@test.com>
32+
Date: Tue, 18 Sep 2012 11:26:11 +0100
33+
Message-ID: <CACm7M_MSh=SkFBnW2t0s6d_rH1e3ntcauoWd2jAO55hwidXEfA@mail.test.com>
34+
Subject: =?ISO-2022-JP?B?GyRCJDMkbCRPJUYlOSVIJEckORsoQg==?=
35+
To: Daniele <daniele@test.com>
36+
Content-Type: multipart/alternative; boundary=047d7b6d8564e71cac04c9f75290
37+
X-Gm-Message-State: ALoCoQlwrVoZ6rkTcekymixxwzWXjBHxmM0yFX4xQ/t74eEEfsoU2nSbGLhgaz8HYimU8WeCI7ll
38+
39+
--047d7b6d8564e71cac04c9f75290
40+
Content-Type: text/plain; charset=ISO-2022-JP
41+
Content-Transfer-Encoding: 7bit
42+
43+
$B$=$l$O:nIJ$r4|BT(B
44+
45+
--047d7b6d8564e71cac04c9f75290
46+
Content-Type: text/html; charset=ISO-2022-JP
47+
Content-Transfer-Encoding: 7bit
48+
49+
$B$=$l$O:nIJ$r4|BT(B<div title="signature"> </div>
50+
51+
--047d7b6d8564e71cac04c9f75290--

0 commit comments

Comments
 (0)