xref: /PHP-8.2/ext/standard/tests/mail/bug80706.phpt (revision 8e2c08c5)
1--TEST--
2Bug #72964 (White space not unfolded for CC/Bcc headers)
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;
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 "X-Mailer header found: ";
40                var_dump(strpos($header, 'X-Mailer: bug80706') !== false);
41                imap_delete($imap_stream, $i);
42                $found = true;
43                break;
44            }
45        }
46        $repeat_count--;
47    }
48
49    imap_close($imap_stream, CL_EXPUNGE);
50    return $found;
51}
52
53$to = "{$users[1]}@$domain";
54$subject = bin2hex(random_bytes(16));
55$message = 'hello';
56$headers = "From: webmaster@example.com\r\n"
57    . "Bcc: {$users[2]}@$domain\r\n"
58    . "X-Mailer: bug80706";
59
60$res = mail($to, $subject, $message, $headers);
61if ($res !== true) {
62	die("TEST FAILED : Unable to send test email\n");
63} else {
64	echo "Message sent OK\n";
65}
66
67foreach ([$users[1], $users[2]] as $user) {
68    if (!find_and_delete_message("$user@$domain", $subject)) {
69        echo "TEST FAILED: email not delivered\n";
70    } else {
71        echo "TEST PASSED: Message sent and deleted OK\n";
72    }
73}
74?>
75--EXPECT--
76Message sent OK
77X-Mailer header found: bool(true)
78TEST PASSED: Message sent and deleted OK
79X-Mailer header found: bool(true)
80TEST PASSED: Message sent and deleted OK
81