最新消息:

php通过imap获取邮件

PHP daimalunzi 105浏览 0评论

首先,下载下来一个第三方的库 https://github.com/SSilence/php-imap-client

其次,把库引入项目中

<?php

namespace program;

require_once “ImapClientException.php”;

require_once “ImapConnect.php”;

require_once “ImapClient.php”;

use SSilence\ImapClient\ImapClientException;

use SSilence\ImapClient\ImapConnect;

use SSilence\ImapClient\ImapClient;

error_reporting(E_ALL || ~E_NOTICE);

ini_set (‘memory_limit’, ‘2048M’);

set_time_limit(0);

然后,把账号导入,按库里面的函数读取即可

例子:

<?

$mailbox = ‘邮箱imap的地址’;

$username = ‘邮箱账号’;

$password = ‘邮箱密码’;

$encryption = null; // or ImapClient::ENCRYPT_SSL or ImapClient::ENCRYPT_TLS or null

$imap = new ImapClient($mailbox, $username, $password, $encryption);

//连接

if($imap->isConnected()===false) {

die($imap->getError());

}else{

echo “ok”;

echo “<hr>”;

}

//获取收件箱

$imap->selectFolder(“Inbox”);

//获取所有邮件数量

$overallMessages = $imap->countMessages();

//获取未读邮件数量

$unreadMessages = $imap->countUnreadMessages();

//获取邮件列表

$emails = $imap->getMessages();

foreach($emails as $k=>$v){

var_dump($v[‘from’]);

echo “<br>”;

var_dump($v[‘to’]);

echo “<br>”;

var_dump($v[‘subject’]);

echo “<br>”;

echo “<br>==================================================================<br>”;

}

转载请注明:PHP学习 » php通过imap获取邮件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址