php imap gmail,如何通过PHP IMAP从Gmail服务器上阅读电子邮件

set_time_limit(4000);

// Connect to gmail

$imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';

$username = 'your_email_id@gmail.com';

$password = 'your_gmail_password';

// try to connect

$inbox = imap_open($imapPath,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* ALL - return all messages matching the rest of the criteria

ANSWERED - match messages with the \\ANSWERED flag set

BCC "string" - match messages with "string" in the Bcc: field

BEFORE "date" - match messages with Date: before "date"

BODY "string" - match messages with "string" in the body of the message

CC "string" - match messages with "string" in the Cc: field

DELETED - match deleted messages

FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set

FROM "string" - match messages with "string" in the From: field

KEYWORD "string" - match messages with "string" as a keyword

NEW - match new messages

OLD - match old messages

ON "date" - match messages with Date: matching "date"

RECENT - match messages with the \\RECENT flag set

SEEN - match messages that have been read (the \\SEEN flag is set)

SINCE "date" - match messages with Date: after "date"

SUBJECT "string" - match messages with "string" in the Subject:

TEXT "string" - match messages with text "string"

TO "string" - match messages with "string" in the To:

UNANSWERED - match messages that have not been answered

UNDELETED - match messages that are not deleted

UNFLAGGED - match messages that are not flagged

UNKEYWORD "string" - match messages that do not have the keyword "string"

UNSEEN - match messages which have not been read yet*/

// search and get unseen emails, function will return email ids

$emails = imap_search($inbox,'UNSEEN');

$output = '';

foreach($emails as $mail) {

$headerInfo = imap_headerinfo($inbox,$mail);

$output .= $headerInfo->subject.'

';

$output .= $headerInfo->toaddress.'

';

$output .= $headerInfo->date.'

';

$output .= $headerInfo->fromaddress.'

';

$output .= $headerInfo->reply_toaddress.'

';

$emailStructure = imap_fetchstructure($inbox,$mail);

if(!isset($emailStructure->parts)) {

$output .= imap_body($inbox, $mail, FT_PEEK);

} else {

//

}

echo $output;

$output = '';

}

// colse the connection

imap_expunge($inbox);

imap_close($inbox);

要使用此功能,您需要安装并启用 php5-imap

您可以使用以下命令安装PHP5 IMAP模块:

apt-get install php5-imap

但是,默认情况下不启用它,因此启用它:

php5enmod imap

要查看更改,请重新启动Apache

service apache2 restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值