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