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