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