1 <?php 2 $array_values['host'] = "host"; 3 $array_values['port'] = 110; 4 $array_values['user'] = 'user name'; 5 $array_values['password'] = 'Password'; 6 $array_values['checkmail'] = 'xxx@xxx.xxx'; 7 8 ganji_get_test_mail($array_values); 9 10 function ganji_get_test_mail($array_values) 11 { 12 $host = $array_values['host']; 13 $port = $array_values['port']; 14 $user = $array_values['user']; 15 $password = $array_values['password']; 16 $checkmail = $array_values['checkmail']; 17 $msg = ''; 18 $return_msg = ''; 19 // ini_set('memory_limit', '80M'); 20 if (! ($sock = fsockopen(gethostbyname($host), $port, $errno, $errstr))) { 21 exit($errno . ': ' . $errstr); 22 } 23 24 set_socket_blocking($sock, true); 25 26 $command = "USER " . $user . "\r\n"; 27 fwrite($sock, $command); 28 $msg = fgets($sock); 29 $command = "PASS " . $password . "\r\n"; 30 fwrite($sock, $command); 31 $msg = fgets($sock); 32 33 $command = "stat\r\n"; 34 fwrite($sock, $command); 35 $return_msg = fgets($sock); 36 37 $msg = fgets($sock); 38 39 $command = "LIST\r\n"; 40 fwrite($sock, $command); 41 $all_mails = array(); 42 while (true) { 43 $msg = fgets($sock); 44 if (! preg_match('/^\+OK/', $msg) && ! preg_match('/^\./', $msg)) { 45 $msg = preg_replace('/\ .*\r\n/', '', $msg); 46 array_push($all_mails, $msg); 47 } 48 if (preg_match('/^\./', $msg)) 49 break; 50 } 51 52 // Get mailing list 53 $ganji_mails = array(); 54 foreach ($all_mails as $item) { 55 fwrite($sock, "TOP $item 0\r\n"); 56 while (true) { 57 $msg = fgets($sock); 58 // echo $msg . "<Br><Br>"; 59 if (preg_match('/^\./', $msg)) { 60 array_push($ganji_mails, $item); 61 break; 62 } 63 } 64 continue; 65 } 66 67 $mail_content = ''; 68 $array_ganji_mails = array(); 69 70 //Traverse line by line 71 foreach ($ganji_mails as $item) { 72 fwrite($sock, "RETR $item\r\n"); 73 while (true) { 74 $msg = fgets($sock); 75 $mail_content .= $msg; 76 if (preg_match('/^\./', $msg)) { 77 array_push($array_ganji_mails, iconv_mime_decode_headers($mail_content, 0, "UTF-8")); 78 $mail_content = ''; 79 break; 80 } 81 } 82 } 83 84 // Get all information of the first email directly 9999 length 85 fwrite($sock, "RETR 1\r\n"); 86 $mail_contents = fread($sock, 9999); // Get all directly 87 88 echo "<br>"; 89 var_dump($mail_contents); 90 echo "<br>"; 91 92 $command = "QUIT\r\n"; 93 fwrite($sock, $command); 94 $msg = fgets($sock); 95 96 return $mail_contents; 97 }
Introduction to common POP3 commands:
Command parameter status description
------------------------------------------
If USER username approves this command and the following pass command, it will lead to state transition
PASS password approval
APOP Name,Digest accept Digest as MD5 message Digest
------------------------------------------
STAT None processes statistics about the mailbox sent back by the requesting server, such as the total number of messages and total bytes
UIDL [Msg ×] processes the unique identifier of the returned message, and each identifier of the POP3 session will be unique
LIST [Msg ×] processes the number of returned messages and the size of each message
RETR [Msg]'s processing returns the entire text of the message identified by the parameter
The DELE [Msg ×] processing server marks messages identified by parameters as deleted and executes the quit command
RSET None processing server will reset all messages marked for deletion to undo the delete command
TOP [Msg]] the processing server will return the first n lines of the message identified by the parameter. N must be a positive integer
NOOP None processes a positive response from the server and does nothing.
------------------------------------------
QUIT None update exit
--------