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