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