PHP使用imap读取QQ邮箱邮件内容及附件

下载receiveMail类

ReceiveMail类

执行方法

/**
 * @param array $param
 * @return array
 * 获取邮箱附件及内容
 */
private function EmailFileOut(&$param = array())
{
    @header('Content-type: text/html;charset=UTF-8');
    $error = ['code' => 0, 'msg' => '', 'num' => 0];
    if (empty($param)) {
        $error['msg'] = "参数错误";
        return $error;
    }
    isset($param['user']) ? $user = $param['user'] : $user = ""; //用户名  邮箱名称 xxxx@qq.com
    isset($param['pass']) ? $pass = $param['pass'] : $pass = ""; //开通imap 的授权码

    if (empty($user) || empty($pass)) {
        $error['msg'] = "用户名或者密码参数错误";
        return $error;
    }

    $host = "imap.qq.com";
    require APP_ROOT . "extensions/pop/receivemail.class.php";
    ignore_user_abort(); // run script in background
    set_time_limit(0); // run script forever
    $fileSavePaht = APP_ROOT . '/upload/test/';
    $obj          = new receiveMail($user, $pass, $user, $host, 'imap', '993', false);
    $conn         = @$obj->connect();
    //连接失败
    if (isset($conn['code'])) {
        $error['msg'] = $conn['msg'];
        return $error;
    }
    $tot = $obj->getTotalMails(); //Total Mails in Inbox Return integer value
    if ($tot < 1) {
        $error['msg'] = "没有获取到邮件";
        $obj->close_mailbox(); //关闭邮箱
        return $error;
    }

    $files = []; //附件列表
    $j     = 0;
    $body  = [];//邮件内容
    $t     = 0;
    for ($i = $tot; $i > 0; $i--) {
        $t++;
        $head = $obj->getHeaders($i);  // Get Header Info Return Array Of Headers **Array Keys are (subject,to,toOth,toNameOth,from,fromName)
        //未读邮件
        //var_dump($head);
        // if ($head['seen']=='U'){
        $str = $obj->GetAttach($i, $fileSavePaht); //获取附件
        // var_dump($str);
        if ($str) {
            $j++;
            $ar    = @explode(",", $str);
            $files = $ar;
        }
        $status = $obj->mailRead($i); //改为已读
        $obj->removeEamil($i);
        //}

        //本项目不需要以下内容
        /*$body[$t-1]['id']=$head['id']; //ID
        $body[$t-1]['date']=$head['date']; //发送时间
        $body[$t-1]['from']=$head['from']; //发件人
        $body[$t-1]['fromName']=$head['fromName']; //发件人名字
        $body[$t-1]['title']=urldecode($head['subject']); //邮件标题
        $body[$t-1]['content']=$obj->getBody($i); //邮件内容*/

    }
    $error['code']   = 1;
    $error['allnum'] = $tot; //总邮件
    $error['num']    = $j; //有附件的邮件
    $error['msg']    = '获取成功';
    $error['file']   = $files;
    // $error['contents']=$body;
    $obj->close_mailbox(); //关闭邮箱

    return $error;
}

执行结果

 $arr['user']="jackhhy520@qq.com";
 $arr['pass']="xxxxxxx"; //你自己的授权码
 $ds= $this->EmailFileOut($arr);    
 var_dump($ds);
 //打印结果
 array(4) {
["result"]=>
bool(true)
["statusCode"]=>
int(200)
["message"]=>
string(12) "请求成功"e
["info"]=>
array(2) {
  ["successCount"]=>
  int(1)
  ["smsIds"]=>
  array(1) {
    [0]=>
    string(51) "1557977436550_103339_13291_28553_jgzvbf$17673135160"
  }
}

评论

(= ̄ω ̄=)··· 暂无内容!

回复

您还未登录,请先登录或者注册

微信扫一扫
关注该公众号