我如何通过我的 PHP 代码访问我的 Gmail 账户? 我需要从我的 Gmail 帐户中获取主题和发件人地址。然后,我需要在 Gmail 上将已访问的邮件标记为已读。 我应该使用 Gmail POP3 客户端吗?是否有任何框架可用于访问 Gmail POP3 服务器。
我只会使用PHP imap函数,然后像下面这样做:
<?php
$mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "USERNAME@googlemail.com", "PASSWORD");
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
这将连接到imap.googlemail.com(googlemail的imap服务器),将$subject设置为第一封邮件的主题,将$from设置为第一封邮件的发件人地址。然后,它将该邮件标记为已读。(未经测试,但它应该能够正常工作:S)
这对我来说可行。
<?php
$yourEmail = "you@gmail.com";
$yourEmailPassword = "your password";
$mailbox = imap_open("{imap.gmail.com:993/ssl}INBOX", $yourEmail, $yourEmailPassword);
$mail = imap_search($mailbox, "ALL");
$mail_headers = imap_headerinfo($mailbox, $mail[0]);
$subject = $mail_headers->subject;
$from = $mail_headers->fromaddress;
imap_setflag_full($mailbox, $mail[0], "\\Seen \\Flagged");
imap_close($mailbox);
?>
<?php
$mbox = imap_open("{imap.example.org:143}", "username", "password")
or die("can't connect: " . imap_last_error());
$status = imap_setflag_full($mbox, "2,5", "\\Seen \\Flagged");
echo gettype($status) . "\n";
echo $status . "\n";
imap_close($mbox);
?>
Zend Framework 还有 Zend_Mail API 用于读取邮件。如果需要切换协议(POP3、IMAP、Mbox 和 Maildir),它使得切换变得容易。目前仅 IMAP 和 Maildir 存储类支持设置标志。
http://framework.zend.com/manual/en/zend.mail.read.html
从Zend Framework文档中阅读消息示例:
$mail = new Zend_Mail_Storage_Pop3(array('host' => 'localhost',
'user' => 'test',
'password' => 'test'));
echo $mail->countMessages() . " messages found\n";
foreach ($mail as $message) {
echo "Mail from '{$message->from}': {$message->subject}\n";
}