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