Skip to content

Commit 1575840

Browse files
00
1 parent 350897e commit 1575840

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ test-polyfill:
8282
@docker-compose run --rm phpunit tests --filter PolyfillTest
8383

8484
test-special:
85-
@docker-compose run --rm phpunit tests --filter ErrorsTest::testWrongImapResourceAsInput
85+
@docker-compose run --rm phpunit tests --filter HeaderInfoTest::testSanitizeAddress

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,5 @@
116116
## Reference
117117

118118
- <https://www.atmail.com/blog/imap-commands/>
119+
- <https://developers.google.com/gmail/imap/xoauth2-protocol>
120+
-

src/HeaderInfo.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class HeaderInfo
1616

1717
public static function fromMessage($message, $defaultHost)
1818
{
19-
#file_put_contents('t3.json', json_encode($message, JSON_PRETTY_PRINT));
19+
file_put_contents('t3.json', json_encode($message, JSON_PRETTY_PRINT));
2020

2121
$replyTo = $message->replyto ?: $message->from;
2222

@@ -26,13 +26,13 @@ public static function fromMessage($message, $defaultHost)
2626
'subject' => $message->subject,
2727
'Subject' => $message->subject,
2828
'message_id' => $message->envelope[9],
29-
'toaddress' => $message->to,
29+
'toaddress' => self::sanitizeAddress($message->to, $defaultHost),
3030
'to' => self::parseAddressList($message->to, $defaultHost),
31-
'fromaddress' => $message->from,
31+
'fromaddress' => self::sanitizeAddress($message->from, $defaultHost),
3232
'from' => self::parseAddressList($message->from, $defaultHost),
33-
'reply_toaddress' => $replyTo ,
33+
'reply_toaddress' => self::sanitizeAddress($replyTo, $defaultHost),
3434
'reply_to' => self::parseAddressList($replyTo, $defaultHost),
35-
'senderaddress' => $message->from,
35+
'senderaddress' => self::sanitizeAddress($message->from, $defaultHost),
3636
'sender' => self::parseAddressList($message->from, $defaultHost),
3737
'Recent' => ' ',
3838
'Unseen' => ' ',
@@ -68,5 +68,17 @@ protected static function parseAddressList($address, $defaultHost)
6868

6969
return $customAddressList;
7070
}
71+
72+
public static function sanitizeAddress($address, $defaultHost)
73+
{
74+
$addressList = imap_rfc822_parse_adrlist($address, $defaultHost);
75+
76+
$sanitizedAddress = [];
77+
foreach ($addressList as $addressEntry) {
78+
$sanitizedAddress[] = imap_rfc822_write_address($addressEntry->mailbox, $addressEntry->host, $addressEntry->personal);
79+
}
80+
81+
return implode(', ', $sanitizedAddress);
82+
}
7183
}
7284

tests/CompatibilityTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,9 @@ public function testHeaderInfo()
464464
foreach ($messageNums as $messageNum) {
465465
$headerInfo1 = imap_headerinfo($imap1, $messageNum);
466466
$headerInfo2 = imap2_headerinfo($imap2, $messageNum);
467-
file_put_contents('t1.json', json_encode($headerInfo1, JSON_PRETTY_PRINT));
468-
file_put_contents('t2.json', json_encode($headerInfo2, JSON_PRETTY_PRINT));
467+
#file_put_contents('t1.json', json_encode($headerInfo1, JSON_PRETTY_PRINT));
468+
#file_put_contents('t2.json', json_encode($headerInfo2, JSON_PRETTY_PRINT));
469+
#die();
469470
$this->assertEquals($headerInfo1, $headerInfo2);
470471
}
471472

tests/HeaderInfoTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Javanile\Imap2\Tests;
4+
5+
use Javanile\Imap2\Connection;
6+
use Javanile\Imap2\HeaderInfo;
7+
use PHPUnit\Framework\Error\Warning;
8+
9+
class HeaderInfoTest extends ImapTestCase
10+
{
11+
public function testSanitizeAddress()
12+
{
13+
$inputs = [
14+
'"TeamViewer Sign In Confirmation" <[email protected]>' => 'TeamViewer Sign In Confirmation <[email protected]>',
15+
'"Aruba.it" <[email protected]>' => '"Aruba.it" <[email protected]>',
16+
];
17+
18+
foreach ($inputs as $input => $output) {
19+
$this->assertEquals($output, HeaderInfo::sanitizeAddress($input, 'localhost'));
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)