• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> php 外部扩展库

    IMAP、POP3、 NNTP(邮件传输协议)

    安装

    要使这些函数生效,你必须在编译 PHP 的时候添加 --with-imap[=DIR] 选项, DIR 表示c-client安装前缀。比如上面提到的例子中,你可以使用 --with-imap=/usr/local/imap-2000b. 根据以上描述,这个路径是指向你先前创建的文件夹。对于 Windows 用户,应该在php.ini文件中引入php_imap.dll.

    Windows 2000之前的系统是不支持的 IMAP 的。这是因为通过SSL连接邮件服务器时使用了加密功能。

    Note: 因为取决于 c-client 是如何配置的,所以你应该向 PHP 配置行添加--with-imap-ssl=/path/to/openssl/ 或者 --with-kerberos=/path/to/kerberos 配置信息。

    Warning

    IMAP,recode,YAZ 和 Cyrus 扩展不能同时使用,因为它们共享了相同 的内部符号。注意:Yaz2.0 及以上版本不存在此问题。

    运行时配置

    这些函数的行为受php.ini中的设置影响。

    IMAP 配置选项
    名字默认可修改范围更新日志
    imap.enable_insecure_rsh"0"PHP_INI_SYSTEMAvailable as of PHP 7.1.25, 7.2.13 and 7.3.0. Formerly, it was implicitly enabled.
    For all the people coming here praying for:
    1) a dead-easy way to read MIME attachments, or
    2) a dead-easy way to access POP3 folders
    Look no further.
    <?php 
    function pop3_login($host,$port,$user,$pass,$folder="INBOX",$ssl=false)
    {
      $ssl=($ssl==false)?"/novalidate-cert":"";
      return (imap_open("{"."$host:$port/pop3$ssl"."}$folder",$user,$pass));
    }
    function pop3_stat($connection)    
    {
      $check = imap_mailboxmsginfo($connection);
      return ((array)$check);
    }
    function pop3_list($connection,$message="")
    {
      if ($message)
      {
        $range=$message;
      } else {
        $MC = imap_check($connection);
        $range = "1:".$MC->Nmsgs;
      }
      $response = imap_fetch_overview($connection,$range);
      foreach ($response as $msg) $result[$msg->msgno]=(array)$msg;
        return $result;
    }
    function pop3_retr($connection,$message)
    {
      return(imap_fetchheader($connection,$message,FT_PREFETCHTEXT));
    }
    function pop3_dele($connection,$message)
    {
      return(imap_delete($connection,$message));
    }
    function mail_parse_headers($headers)
    {
      $headers=preg_replace('/\r\n\s+/m', '',$headers);
      preg_match_all('/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)?\r\n/m', $headers, $matches);
      foreach ($matches[1] as $key =>$value) $result[$value]=$matches[2][$key];
      return($result);
    }
    function mail_mime_to_array($imap,$mid,$parse_headers=false)
    {
      $mail = imap_fetchstructure($imap,$mid);
      $mail = mail_get_parts($imap,$mid,$mail,0);
      if ($parse_headers) $mail[0]["parsed"]=mail_parse_headers($mail[0]["data"]);
      return($mail);
    }
    function mail_get_parts($imap,$mid,$part,$prefix)
    {  
      $attachments=array();
      $attachments[$prefix]=mail_decode_part($imap,$mid,$part,$prefix);
      if (isset($part->parts)) // multipart
      {
        $prefix = ($prefix == "0")?"":"$prefix.";
        foreach ($part->parts as $number=>$subpart) 
          $attachments=array_merge($attachments, mail_get_parts($imap,$mid,$subpart,$prefix.($number+1)));
      }
      return $attachments;
    }
    function mail_decode_part($connection,$message_number,$part,$prefix)
    {
      $attachment = array();
      if($part->ifdparameters) {
        foreach($part->dparameters as $object) {
          $attachment[strtolower($object->attribute)]=$object->value;
          if(strtolower($object->attribute) == 'filename') {
            $attachment['is_attachment'] = true;
            $attachment['filename'] = $object->value;
          }
        }
      }
      if($part->ifparameters) {
        foreach($part->parameters as $object) {
          $attachment[strtolower($object->attribute)]=$object->value;
          if(strtolower($object->attribute) == 'name') {
            $attachment['is_attachment'] = true;
            $attachment['name'] = $object->value;
          }
        }
      }
      $attachment['data'] = imap_fetchbody($connection, $message_number, $prefix);
      if($part->encoding == 3) { // 3 = BASE64
        $attachment['data'] = base64_decode($attachment['data']);
      }
      elseif($part->encoding == 4) { // 4 = QUOTED-PRINTABLE
        $attachment['data'] = quoted_printable_decode($attachment['data']);
      }
      return($attachment);
    }
    ?>
    [EDIT BY danbrown AT php DOT net: Contains a bugfix by "mn26826" on 09-JUN-2010, which fixed the erroneous reference to $imap as the parameter passed to imap_mailboxmsginfo() within the user function pop3_stat(). This was intended to be $connection.]
    [EDIT BY visualmind AT php DOT net: Contains a bugfix by "elias-jobview" on 17-AUG-2010, which fixed the error in pop3_list function which didn't have: return $result]
    [EDIT BY danbrown AT php DOT net: Contains a bugfix by "chrismeistre" on 09-SEP-2010, which fixed the erroneous reference to $mbox (should be $connection) in the pop3_list() function.]