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