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