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