PHP - imap_last_error() 函数



PHP 的 IMAP 函数可以帮助您访问电子邮件帐户。IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol。使用这些函数,您还可以处理 NNTP、POP3 协议和本地邮箱访问方法。imap_last_error() 函数检索并返回当前页面中发生的最后一个错误消息的完整文本。

语法

imap_last_error();

参数

此函数不接受任何参数。

返回值

如果成功,此函数返回包含最后发生错误消息的字符串值;如果失败,则返回布尔值 FALSE。

PHP 版本

此函数首次在 PHP 4 版本中引入,并在所有后续版本中均有效。

示例

以下示例演示了 imap_last_error() 函数的使用:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "wrong_password";
         $mailbox = imap_open($url, $id, $pwd);
         print("<br>");
         exit ("Errors: <br>");
         
         if ( $mailbox === false ) {
            print(imap_last_error());      
         } else {
            print("Connection established....");
         }
      ?>
   </body>
</html>

输出

这将生成以下输出:

Warning: imap_open(): Couldn't open stream 
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX in 
C:\Apache24\htdocs\examples\demo2.html on line 7

Errors:

Notice: Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0

Notice: Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0

Notice: Unknown: [AUTHENTICATIONFAILED] Invalid credentials (Failure) (errflg=1) in Unknown on line 0

Notice: Unknown: Too many login failures (errflg=2) in Unknown on line 0 

示例

以下是此函数的另一个示例:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         print("Connection established....");
         print("<br>");

         //Creating mailboxes
         $newmailbox1 = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.test_mail1";
         $newmailbox2 = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.test_mail2";            
         $res = imap_createmailbox($mailbox, imap_utf7_encode($newmailbox1));
         $res = imap_createmailbox($mailbox, imap_utf7_encode($newmailbox2));

         //Retrieving the contents of mail boxes
         print("List of mailboxes: ");
         print("<br>");
         $list = imap_getmailboxes($mailbox, $url, "*");
         print("Errors: <br>");    
         foreach ($list as $key => $val) {
            print_r($val->name);
            print("<br>");
            print(imap_last_error());			
         }
      ?>
   </body>
</html>

输出

这将生成以下输出:

Connection established....
List of mailboxes:
Errors:
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX

Notice: Array to string conversion in C:\Apache24\htdocs\examples\trail.html on line 25
Array{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail1
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail2
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.de
php_function_reference.htm
广告