1--TEST--
2Test mail() function : basic functionality
3--EXTENSIONS--
4imap
5--CONFLICTS--
6imap
7--SKIPIF--
8<?php
9if( substr(PHP_OS, 0, 3) != 'WIN' ) {
10   die('skip...Windows only test');
11}
12
13require_once(__DIR__.'/mail_skipif.inc');
14?>
15--INI--
16max_execution_time = 120
17--FILE--
18<?php
19ini_set("SMTP", "localhost");
20ini_set("smtp_port", 2525);
21ini_set("sendmail_from", "user@example.com");
22
23echo "*** Testing mail() : basic functionality ***\n";
24require_once(__DIR__.'/mail_include.inc');
25$subject_prefix = "!**PHPT**!";
26
27$to = "$username";
28$subject = "$subject_prefix: Basic PHPT test for mail() function";
29$message = <<<HERE
30Description
31bool mail ( string \$to , string \$subject , string \$message [, string \$additional_headers [, string \$additional_parameters]] )
32Send an email message
33HERE;
34
35$res = mail($to, $subject, $message);
36
37if ($res !== true) {
38    exit("TEST COMPLETED : Unable to send test email\n");
39} else {
40    echo "Msg sent OK\n";
41}
42
43// Search for email message on the mail server using imap.
44$imap_stream = imap_open($default_mailbox, $username, $password);
45if ($imap_stream === false) {
46    echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
47    return false;
48}
49
50$found = false;
51$repeat_count = 20; // we will repeat a max of 20 times
52while (!$found && $repeat_count > 0) {
53
54    // sleep for a while to allow msg to be delivered
55    sleep(1);
56
57    $current_msg_count = imap_check($imap_stream)->Nmsgs;
58
59    // Iterate over recent msgs to find the one we sent above
60    for ($i = 1; $i <= $current_msg_count; $i++) {
61        // get hdr details
62        $hdr = imap_headerinfo($imap_stream, $i);
63
64        if (substr($hdr->Subject, 0 , strlen($subject_prefix)) == $subject_prefix) {
65            echo "Id of msg just sent is $i\n";
66            echo ".. delete it\n";
67            imap_delete($imap_stream, $i);
68            $found = true;
69            break;
70        }
71    }
72
73    $repeat_count -= 1;
74}
75
76if (!$found) {
77    echo "TEST FAILED: email not delivered\n";
78} else {
79    echo "TEST PASSED: Msgs sent and deleted OK\n";
80}
81
82imap_close($imap_stream, CL_EXPUNGE);
83?>
84===DONE===
85--EXPECTF--
86*** Testing mail() : basic functionality ***
87
88Warning: mail(): Failed to connect to mailserver at "localhost" port 2525, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in %s on line %d
89TEST COMPLETED : Unable to send test email
90