设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 442|回复: 1

PHP to deal with POP3 mail server

[复制链接]
发表于 2015-6-11 11:56:22 | 显示全部楼层 |阅读模式
本帖最后由 demo 于 2015-6-12 04:12 编辑

PHP to establish connections with a POP3 mail server amd be able to list, retrieve and delete mail messages from a given mail box.

  1. <?

  2. /*
  3. * Copyright (C) 1998, Manuel Lemos (mlemos@acm.org)
  4. *
  5. */

  6. /* put this class definition in a file named pop3.php */

  7. class pop3_class
  8. {
  9.     var $hostname = "";
  10.     var $port = 110;
  11.    
  12.     /* Private variables - DO NOT ACCESS */
  13.    
  14.     var $connection = 0;
  15.     var $state = "DISCONNECTED";
  16.     var $greeting = "";
  17.     var $must_update = 0;
  18.    
  19.    
  20.     /* Private methods - DO NOT CALL */
  21.    
  22.     Function GetLine()
  23.     {
  24.         for ($line = "";;) {
  25.             if (feof($this->connection))
  26.                 return (0);
  27.             $line .= fgets($this->connection, 100);
  28.             $length = strlen($line);
  29.             if ($length >= 2 && substr($line, $length - 2, 2) == "\r\n")
  30.                 return (substr($line, 0, $length - 2));
  31.         }
  32.     }
  33.    
  34.     Function PutLine($line)
  35.     {
  36.         return (fputs($this->connection, "$line\r\n"));
  37.     }
  38.    
  39.     Function OpenConnection()
  40.     {
  41.         if ($this->hostname == "")
  42.             return ("2 it was not specified a valid hostname");
  43.         switch (($this->connection = fsockopen($this->hostname, $this->port))) {
  44.             case -3:
  45.                 return ("-3 socket could not be created");
  46.             case -4:
  47.                 return ("-4 dns lookup on hostname "$hostname" failed");
  48.             case -5:
  49.                 return ("-5 connection refused or timed out");
  50.             case -6:
  51.                 return ("-6 fdopen() call failed");
  52.             case -7:
  53.                 return ("-7 setvbuf() call failed");
  54.             default:
  55.                 return ("");
  56.         }
  57.     }
  58.    
  59.     Function CloseConnection()
  60.     {
  61.         if ($this->connection != 0) {
  62.             fclose($this->connection);
  63.             $this->connection = 0;
  64.         }
  65.     }
  66.    
  67.     /* Public methods */
  68.    
  69.     /* Open method - set the object variable $hostname to the POP3 server address. */
  70.    
  71.     Function Open()
  72.     {
  73.         if ($this->state != "DISCONNECTED")
  74.             return ("1 a connection is already opened");
  75.         if (($error = $this->OpenConnection()) != "")
  76.             return ($error);
  77.         $this->greeting = $this->GetLine();
  78.         if (GetType($this->greeting) != "string" || strtok($this->greeting, " ") != "+OK") {
  79.             $this->CloseConnection();
  80.             return ("3 POP3 server greeting was not found");
  81.         }
  82.         $this->greeting    = strtok("\r\n");
  83.         $this->must_update = 0;
  84.         $this->state       = "AUTHORIZATION";
  85.         return ("");
  86.     }
  87.    
  88.     /* Close method - this method must be called at least if there are any
  89.     messages to be deleted */
  90.    
  91.     Function Close()
  92.     {
  93.         if ($this->state == "DISCONNECTED")
  94.             return ("no connection was opened");
  95.         if ($this->must_update) {
  96.             if ($this->PutLine("QUIT") == 0)
  97.                 return ("Could not send the QUIT command");
  98.             $response = $this->GetLine();
  99.             if (GetType($response) != "string")
  100.                 return ("Could not get quit command response");
  101.             if (strtok($response, " ") != "+OK")
  102.                 return ("Could not quit the connection: " . strtok("\r\n"));
  103.         }
  104.         $this->CloseConnection();
  105.         $this->state = "DISCONNECTED";
  106.         return ("");
  107.     }
  108.    
  109.     /* Login method - pass the user name and password of POP account. Set
  110.     $apop to 1 or 0 wether you want to login using APOP method or not. */
  111.    
  112.     Function Login($user, $password, $apop)
  113.     {
  114.         if ($this->state != "AUTHORIZATION")
  115.             return ("connection is not in AUTHORIZATION state");
  116.         if ($apop) {
  117.             if ($this->PutLine("APOP $user " . md5($this->greeting . $password)) == 0)
  118.                 return ("Could not send the APOP command");
  119.             $response = $this->GetLine();
  120.             if (GetType($response) != "string")
  121.                 return ("Could not get APOP login command response");
  122.             if (strtok($response, " ") != "+OK")
  123.                 return ("APOP login failed: " . strtok("\r\n"));
  124.         } else {
  125.             if ($this->PutLine("USER $user") == 0)
  126.                 return ("Could not send the USER command");
  127.             $response = $this->GetLine();
  128.             if (GetType($response) != "string")
  129.                 return ("Could not get user login entry response");
  130.             if (strtok($response, " ") != "+OK")
  131.                 return ("User error: " . strtok("\r\n"));
  132.             if ($this->PutLine("PASS $password") == 0)
  133.                 return ("Could not send the PASS command");
  134.             $response = $this->GetLine();
  135.             if (GetType($response) != "string")
  136.                 return ("Could not get login password entry response");
  137.             if (strtok($response, " ") != "+OK")
  138.                 return ("Password error: " . strtok("\r\n"));
  139.         }
  140.         $this->state = "TRANSACTION";
  141.         return ("");
  142.     }
  143.    
  144.     /* Statistics method - pass references to variables to hold the number of
  145.     messages in the mail box and the size that they take in bytes. */
  146.    
  147.     Function Statistics($messages, $size)
  148.     {
  149.         if ($this->state != "TRANSACTION")
  150.             return ("connection is not in TRANSACTION state");
  151.         if ($this->PutLine("STAT") == 0)
  152.             return ("Could not send the STAT command");
  153.         $response = $this->GetLine();
  154.         if (GetType($response) != "string")
  155.             return ("Could not get the statistics command response");
  156.         if (strtok($response, " ") != "+OK")
  157.             return ("Could not get the statistics: " . strtok("\r\n"));
  158.         $messages = strtok(" ");
  159.         $size     = strtok(" ");
  160.         return ("");
  161.     }
  162.    
  163.     /* ListMessages method - the $message argument indicates the number of a
  164.     message to be listed. If you specify an empty string it will list all
  165.     messages in the mail box. The $unique_id flag indicates if you want
  166.     to list the each message unique identifier, otherwise it will
  167.     return the size of each message listed. If you list all messages the
  168.     result will be returned in an array. */
  169.    
  170.     Function ListMessages($message, $unique_id)
  171.     {
  172.         if ($this->state != "TRANSACTION")
  173.             return ("connection is not in TRANSACTION state");
  174.         if ($unique_id)
  175.             $list_command = "UIDL";
  176.         else
  177.             $list_command = "LIST";
  178.         if ($this->PutLine("$list_command $message") == 0)
  179.             return ("Could not send the $list_command command");
  180.         $response = $this->GetLine();
  181.         if (GetType($response) != "string")
  182.             return ("Could not get message list command response");
  183.         if (strtok($response, " ") != "+OK")
  184.             return ("Could not get the message listing: " . strtok("\r\n"));
  185.         if ($message == "") {
  186.             for ($messages = array();;) {
  187.                 $response = $this->GetLine();
  188.                 if (GetType($response) != "string")
  189.                     return ("Could not get message list response");
  190.                 if ($response == ".")
  191.                     break;
  192.                 $message = intval(strtok($response, " "));
  193.                 if ($unique_id)
  194.                     $messages[$message] = strtok(" ");
  195.                 else
  196.                     $messages[$message] = intval(strtok(" "));
  197.             }
  198.             return ($messages);
  199.         } else {
  200.             $message = intval(strtok(" "));
  201.             return (intval(strtok(" ")));
  202.         }
  203.     }
  204.    

复制代码

 楼主| 发表于 2015-6-11 11:58:59 | 显示全部楼层
本帖最后由 demo 于 2015-6-12 04:13 编辑

Continue
  1.     /* RetrieveMessage method - the $message argument indicates the number of
  2.     a message to be listed. Pass a reference variables that will hold the
  3.     arrays of the $header and $body lines. The $lines argument tells how
  4.     many lines of the message are to be retrieved. Pass a negative number
  5.     if you want to retrieve the whole message. */
  6.    
  7.     Function RetrieveMessage($message, $headers, $body, $lines)
  8.     {
  9.         if ($this->state != "TRANSACTION")
  10.             return ("connection is not in TRANSACTION state");
  11.         if ($lines < 0) {
  12.             $command   = "RETR";
  13.             $arguments = "$message";
  14.         } else {
  15.             $command   = "TOP";
  16.             $arguments = "$message $lines";
  17.         }
  18.         if ($this->PutLine("$command $arguments") == 0)
  19.             return ("Could not send the $command command");
  20.         $response = $this->GetLine();
  21.         if (GetType($response) != "string")
  22.             return ("Could not get message retrieval command response");
  23.         if (strtok($response, " ") != "+OK")
  24.             return ("Could not retrieve the message: " . strtok("\r\n"));
  25.         for ($headers = $body = array(), $line = 0;; $line++) {
  26.             $response = $this->GetLine();
  27.             if (GetType($response) != "string")
  28.                 return ("Could not retrieve the message");
  29.             switch ($response) {
  30.                 case ".":
  31.                     return ("");
  32.                 case "":
  33.                     break 2;
  34.                 default:
  35.                     if (substr($response, 0, 1) == ".")
  36.                         $response = substr($response, 1, strlen($response) - 1);
  37.                     break;
  38.             }
  39.             $headers[$line] = $response;
  40.         }
  41.         for ($line = 0;; $line++) {
  42.             $response = $this->GetLine();
  43.             if (GetType($response) != "string")
  44.                 return ("Could not retrieve the message");
  45.             switch ($response) {
  46.                 case ".":
  47.                     return ("");
  48.                 default:
  49.                     if (substr($response, 0, 1) == ".")
  50.                         $response = substr($response, 1, strlen($response) - 1);
  51.                     break;
  52.             }
  53.             $body[$line] = $response;
  54.         }
  55.         return ("");
  56.     }
  57.    
  58.     /* DeleteMessage method - the $message argument indicates the number of
  59.     a message to be marked as deleted. Messages will only be effectively
  60.     deleted upon a successful call to the Close method. */
  61.    
  62.     Function DeleteMessage($message)
  63.     {
  64.         if ($this->state != "TRANSACTION")
  65.             return ("connection is not in TRANSACTION state");
  66.         if ($this->PutLine("DELE $message") == 0)
  67.             return ("Could not send the DELE command");
  68.         $response = $this->GetLine();
  69.         if (GetType($response) != "string")
  70.             return ("Could not get message delete command response");
  71.         if (strtok($response, " ") != "+OK")
  72.             return ("Could not delete the message: " . strtok("\r\n"));
  73.         $this->must_update = 1;
  74.         return ("");
  75.     }
  76.    
  77.     /* ResetDeletedMessages method - Reset the list of marked to be deleted
  78.     messages. No messages will be marked to be deleted upon a successful
  79.     call to this method. */
  80.    
  81.     Function ResetDeletedMessages()
  82.     {
  83.         if ($this->state != "TRANSACTION")
  84.             return ("connection is not in TRANSACTION state");
  85.         if ($this->PutLine("RSET") == 0)
  86.             return ("Could not send the RSET command");
  87.         $response = $this->GetLine();
  88.         if (GetType($response) != "string")
  89.             return ("Could not get reset deleted messages command response");
  90.         if (strtok($response, " ") != "+OK")
  91.             return ("Could not reset deleted messages: " . strtok("\r\n"));
  92.         $this->must_update = 0;
  93.         return ("");
  94.     }
  95.    
  96.     /* IssueNOOP method - Just pings the server to prevent it auto-close the
  97.     connection after an idle timeout (tipically 10 minutes). Not very
  98.     useful for most likely uses of this class. It's just here for
  99.     protocol support completeness. */
  100.    
  101.     Function IssueNOOP()
  102.     {
  103.         if ($this->state != "TRANSACTION")
  104.             return ("connection is not in TRANSACTION state");
  105.         if ($this->PutLine("NOOP") == 0)
  106.             return ("Could not send the NOOP command");
  107.         $response = $this->GetLine();
  108.         if (GetType($response) != "string")
  109.             return ("Could not NOOP command response");
  110.         if (strtok($response, " ") != "+OK")
  111.             return ("Could not issue the NOOP command: " . strtok("\r\n"));
  112.         return ("");
  113.     }
  114. }
  115. ;

  116. /* ---- pop3.php3 class file ends here. ---- */

  117. ?>
复制代码



---- example file ----

  1. <HTML>
  2. <HEAD>
  3. <TITLE>POP3 PHP class test</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <?
  7. include("pop3.php3");

  8. $user                      = "mlemos";
  9. $password                  = "password";
  10. $apop                      = 0;
  11. $pop3_connection           = new pop3_class;
  12. $pop3_connection->hostname = "localhost";
  13. if (($error = $pop3_connection->Open()) == "") {
  14.     echo "<PRE>Connected to the POP3 server $pop3_connection->hostname</PRE>\n";
  15.     if (($error = $pop3_connection->Login($user, $password, $apop)) == "") {
  16.         echo "<PRE>User $user logged in.</PRE>\n";
  17.         if (($error = $pop3_connection->Statistics(&$messages, &$size)) == "") {
  18.             echo "<PRE>There are $messages messages in the mail box with a total of $size bytes.</PRE>\n";
  19.             $result = $pop3_connection->ListMessages("", 0);
  20.             if (GetType($result) == "array") {
  21.                 for (Reset($result), $message = 0; $message < count($result); Next($result), $message++)
  22.                     echo "<PRE>Message ,Key($result), - , " . $result[Key($result)] . " , bytes.</PRE>\n";
  23.                 $result = $pop3_connection->ListMessages("", 1);
  24.                 if (GetType($result) == "array") {
  25.                     for (Reset($result), $message = 0; $message < count($result); Next($result), $message++)
  26.                         echo "<PRE>Message ,Key($result), Unique ID - "" . $result[Key($result)] . ""</PRE>\n";
  27.                     if ($messages > 0) {
  28.                         if (($error = $pop3_connection->RetrieveMessage(1, &$headers, &$body, 2)) == "") {
  29.                             echo "<PRE>Message 1:\n---Message headers starts below---</PRE>\n";
  30.                             for ($line = 0; $line < count($headers); $line++)
  31.                                 echo "<PRE>", HtmlSpecialChars($headers[$line]), "</PRE>\n";
  32.                             echo "<PRE>---Message headers ends above---\n---Message body starts below---</PRE>\n";
  33.                             for ($line = 0; $line < count($body); $line++)
  34.                                 echo "<PRE>", HtmlSpecialChars($body[$line]), "</PRE>\n";
  35.                             echo "<PRE>---Message body ends above---</PRE>\n";
  36.                             if (($error = $pop3_connection->DeleteMessage(1)) == "") {
  37.                                 echo "<PRE>Marked message 1 for deletion.</PRE>\n";
  38.                                 if (($error = $pop3_connection->ResetDeletedMessages()) == "") {
  39.                                     echo "<PRE>Resetted the list of messages to be deleted.</PRE>\n";
  40.                                 }
  41.                             }
  42.                         }
  43.                     }
  44.                     if ($error == "" && ($error = $pop3_connection->Close()) == "")
  45.                         echo "<PRE>Disconnected from the POP3 server $pop3_connection->hostname</PRE>\n";
  46.                     
  47.                 } else
  48.                     $error = $result;
  49.             } else
  50.                 $error = $result;
  51.         }
  52.     }
  53. }
  54. if ($error != "")
  55.     echo "<H2>Error: ", HtmlSpecialChars($error), "</H2>";
  56. ?>

  57. </BODY>
  58. </HTML>
复制代码



reference: http://www.weberdev.com/get_example.php3?ExampleID=461
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 11:07 , Processed in 0.014527 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表