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