PHP - imap_open() 函式



PHP 的 IMAP 函式幫助您訪問電子郵件帳戶,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用這些函式您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

**imap_open()** 函式接受三個字串值作為引數,分別表示郵箱名稱/URL、使用者名稱和密碼,並開啟到指定郵箱的流。

語法

imap_open ($mailbox, $username, $password [$options, $n_retries, $params);

引數

序號 引數及描述
1

郵箱 (必填)

這是一個字串值,表示郵箱的名稱/URL。它包含伺服器名稱和郵箱路徑。

2

使用者名稱 (必填)

這是一個字串值,表示使用者名稱。

3

密碼 (必填)

這是一個字串值,表示密碼。

4

選項 (可選)

這是一個整數值,表示可選引數,可以是以下一個或多個:-

  • OP_READONLY

  • OP_ANONYMOUS

  • OP_HALFOPEN

  • CL_EXPUNGE

  • OP_DEBUG

  • OP_SHORTCACHE

  • OP_SILENT

  • OP_PROTOTYPE

  • OP_SECURE

5

重試次數 (可選)

這是一個整數值,表示最大嘗試次數。

6

引數 (可選)

這是一個數組值,表示連線引數。

返回值

如果成功,此函式返回一個 IMAP 流物件;如果失敗,則返回布林值 FALSE。

PHP 版本

此函式首次引入於 PHP 4 版本,並在所有後續版本中均有效。

示例

以下是一個嘗試使用 **imap_open()** 建立與特定 Gmail 帳戶連線的 PHP 程式:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         if($mailbox){
            print("Connection established....");
         } else {
            print("Connection failed");
         }
      ?>
   </body>
</html>

輸出

以上程式生成以下輸出:

Connection established....

示例

以下是此函式的另一個示例,在這個示例中,我們已經連線到一個特定的郵箱並檢索其中郵件的內容。

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
         //Searching emails
         $emailData = imap_search($imap, '');
        
         if (! empty($emailData)) {  
            foreach ($emailData as $msg) {
               $msg = imap_fetchbody($imap, $msg, "1");
               print(quoted_printable_decode($msg)."<br>");                
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

以上程式生成以下輸出:

Connection established....
This is a test mail #1.
--0000000000001bf26805af905277 Content-Type: text/plain; charset="UTF-8" test 
mail #2 --0000000000001bf26805af905277 Content-Type: text/html; charset="UTF-8" 
Content-Transfer-Encoding: quoted-printable

test mail #2
--0000000000001bf26805af905277--
test mail #3
test mail #4

示例

以下是帶可選引數的此函式示例。

<html>
   <body>
      <?php
         //Establishing the connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         
         //Optional parameters
         $options = OP_READONLY;
         $retries = 10;
		 
         $mailbox = imap_open($url, $id, $pwd, $options, $retries);
         if($mailbox){
            print("Connection established....");
         } else {
            print("Connection failed");
         }
      ?>
   </body>
</html>

以上程式生成以下輸出:

Connection established....
php_function_reference.htm
廣告