xref: /PHP-8.2/ext/standard/tests/mail/bug80751.phpt (revision 8e2c08c5)
1--TEST--
2Bug #80751 (Comma in recipient name breaks email delivery)
3--EXTENSIONS--
4imap
5--CONFLICTS--
6imap
7--SKIPIF--
8<?php
9if (PHP_OS_FAMILY !== 'Windows') die('skip Windows only test');
10if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
11require_once __DIR__ . '/mail_skipif.inc';
12?>
13--INI--
14SMTP=localhost
15smtp_port=25
16--FILE--
17<?php
18require_once __DIR__ . '/mail_include.inc';
19
20function find_and_delete_message($username, $subject) {
21    global $default_mailbox, $password, $users, $domain;
22
23    $imap_stream = imap_open($default_mailbox, $username, $password);
24    if ($imap_stream === false) {
25        die("Cannot connect to IMAP server $server: " . imap_last_error() . "\n");
26    }
27
28    $found = false;
29    $repeat_count = 20; // we will repeat a max of 20 times
30    while (!$found && $repeat_count > 0) {
31        // sleep for a while to allow msg to be delivered
32        sleep(1);
33
34        $num_messages = imap_check($imap_stream)->Nmsgs;
35        for ($i = $num_messages; $i > 0; $i--) {
36            $info = imap_headerinfo($imap_stream, $i);
37            if ($info->subject === $subject) {
38                $header = imap_fetchheader($imap_stream, $i);
39                echo "Return-Path header found: ";
40                var_dump(strpos($header, 'Return-Path: joe@example.com') !== false);
41                echo "To header found: ";
42                var_dump(strpos($header, "To: \"<bob@example.com>\" <{$users[1]}@$domain>") !== false);
43                echo "From header found: ";
44                var_dump(strpos($header, 'From: "<bob@example.com>" <joe@example.com>') !== false);
45                echo "Cc header found: ";
46                var_dump(strpos($header, "Cc: \"Lastname, Firstname\\\\\" <{$users[2]}@$domain>") !== false);
47                imap_delete($imap_stream, $i);
48                $found = true;
49                break;
50            }
51        }
52        $repeat_count--;
53    }
54
55    imap_close($imap_stream, CL_EXPUNGE);
56    return $found;
57}
58
59$to = "\"<bob@example.com>\" <{$users[1]}@$domain>";
60$subject = bin2hex(random_bytes(16));
61$message = 'hello';
62$headers = "From: \"<bob@example.com>\" <joe@example.com>\r\n"
63    . "Cc: \"Lastname, Firstname\\\\\" <{$users[2]}@$domain>\r\n"
64    . "Bcc: \"Firstname \\\"Ni,ck\\\" Lastname\" <{$users[3]}@$domain>\r\n";
65
66$res = mail($to, $subject, $message, $headers);
67if ($res !== true) {
68	die("TEST FAILED : Unable to send test email\n");
69} else {
70	echo "Message sent OK\n";
71}
72
73foreach ([$users[1], $users[2], $users[3]] as $user) {
74    if (!find_and_delete_message("$user@$domain", $subject)) {
75        echo "TEST FAILED: email not delivered\n";
76    } else {
77        echo "TEST PASSED: Message sent and deleted OK\n";
78    }
79}
80?>
81--EXPECT--
82Message sent OK
83Return-Path header found: bool(true)
84To header found: bool(true)
85From header found: bool(true)
86Cc header found: bool(true)
87TEST PASSED: Message sent and deleted OK
88Return-Path header found: bool(true)
89To header found: bool(true)
90From header found: bool(true)
91Cc header found: bool(true)
92TEST PASSED: Message sent and deleted OK
93Return-Path header found: bool(true)
94To header found: bool(true)
95From header found: bool(true)
96Cc header found: bool(true)
97TEST PASSED: Message sent and deleted OK
98