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
19echo "*** Testing mail() : basic functionality ***\n";
20require_once(__DIR__.'/mail_include.inc');
21$subject_prefix = "!**PHPT**!";
22
23$to = "$username";
24$subject = "$subject_prefix: Basic PHPT test for mail() function";
25$message = <<<HERE
26Description
27bool mail ( string \$to , string \$subject , string \$message [, string \$additional_headers [, string \$additional_parameters]] )
28Send an email message
29HERE;
30
31$extra_headers = "from: user@example.com";
32$extra_parameters = "addons"; // should be ignored
33
34$res = mail($to, $subject, $message, $extra_headers, $extra_parameters);
35
36if ($res !== true) {
37    exit("TEST FAILED : Unable to send test email\n");
38} else {
39    echo "Msg sent OK\n";
40}
41
42// Search for email message on the mail server using imap.
43$imap_stream = imap_open($default_mailbox, $username, $password);
44if ($imap_stream === false) {
45    echo "Cannot connect to IMAP server $server: " . imap_last_error() . "\n";
46    return false;
47}
48
49$found = false;
50$repeat_count = 20; // we will repeat a max of 20 times
51while (!$found && $repeat_count > 0) {
52
53    // sleep for a while to allow msg to be delivered
54    sleep(1);
55
56    $current_msg_count = imap_check($imap_stream)->Nmsgs;
57
58    // Iterate over recent msgs to find the one we sent above
59    for ($i = 1; $i <= $current_msg_count; $i++) {
60        // get hdr details
61        $hdr = imap_headerinfo($imap_stream, $i);
62
63        if (substr($hdr->Subject, 0 , strlen($subject_prefix)) == $subject_prefix) {
64            echo "Id of msg just sent is $i\n";
65            echo ".. delete it\n";
66            imap_delete($imap_stream, $i);
67            $found = true;
68            break;
69        }
70    }
71
72    $repeat_count -= 1;
73}
74
75if (!$found) {
76    echo "TEST FAILED: email not delivered\n";
77} else {
78    echo "TEST PASSED: Msgs sent and deleted OK\n";
79}
80
81imap_close($imap_stream, CL_EXPUNGE);
82?>
83--EXPECTF--
84*** Testing mail() : basic functionality ***
85Msg sent OK
86Id of msg just sent is %d
87.. delete it
88TEST PASSED: Msgs sent and deleted OK
89