當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imap_open()用法及代碼示例


PHP-IMAP 函數可幫助您訪問電子郵件帳戶,IMAP 代表I互聯網M艾爾A訪問權限Protocol 使用這些函數您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

這個imap_open()函數接受代表郵箱名稱/URL、用戶名和密碼的三個字符串值作為參數,並打開指定郵箱的流。

用法

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

參數

Sr.No 參數及說明
1

mailbox(Mandatory)

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

2

username(Mandatory)

這是一個表示用戶名的字符串值。

3

password(Mandatory)

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

4

options (Optional)

這是一個表示可選參數的整數值,它可以是以下一個或多個 -

  • OP_READONLY

  • OP_ANONYMOUS

  • OP_HALFOPEN

  • CL_EXPUNGE

  • OP_DEBUG

  • OP_SHORTCACHE

  • OP_SILENT

  • OP_PROTOTYPE

  • OP_SECURE

5

retries (Optional)

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

6

params (Optional)

這是一個表示連接參數的數組值。

返回值

該函數在成功的情況下返回一個 IMAP 流對象,在失敗的情況下返回一個布爾值 FALSE。

PHP版本

這個函數最初是在 PHP 版本 4 中引入的,並且適用於所有後續版本。

示例

以下是一個 php 程序嘗試與特定的 Gmail 帳戶建立連接imap_open()-

<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 - imap_open() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。