通过 php 代码访问我的 Gmail 收件箱

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1385797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:19:09  来源:igfitidea点击:

accessing my gmail inbox via php code

phpemailgmailpop3

提问by user156073

how i can access my gmail account through my php code? I need to get the subject and the from address to from my gmail account.And then i need to mark the accessed as read on gmail Should i use gmail pop3 clint?is that any framework that i can use for accessing gmail pop3 server.

我如何通过我的 php 代码访问我的 gmail 帐户?我需要从我的 gmail 帐户中获取主题和发件人地址。然后我需要在 gmail 上将访问标记为已读我应该使用 gmail pop3 clint 吗?是我可以用来访问 gmail pop3 服务器的任何框架。

回答by Jerry

I would just use the PHP imap functionsand do something like this:

我只会使用PHP imap 函数并执行以下操作:

<?php
    $mailbox = imap_open("{imap.googlemail.com:993/ssl}INBOX", "[email protected]", "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);
?>

This connects to imap.googlemail.com (googlemail's imap server), sets $subject to the subject of the first message and $from to the from address of the first message. Then, it marks this message as read. (It's untested, but it shouldwork :S)

这会连接到 imap.googlemail.com(googlemail 的 imap 服务器),将 $subject 设置为第一封邮件的主题,将 $from 设置为第一封邮件的发件人地址。然后,它将此消息标记为已读。(它未经测试,但它应该可以工作:S)

回答by Floyd

This works for me.

这对我有用。

<?php

$yourEmail = "[email protected]";
$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);
?>

回答by Riz

Another nice IMAP example is available at http://davidwalsh.name/gmail-php-imap

另一个不错的 IMAP 示例可在http://davidwalsh.name/gmail-php-imap 获得

回答by stribika

You can use IMAPfrom PHP.

您可以从 PHP使用IMAP

<?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);
?>

回答by Chris Martin

Zend Framework has the Zend_Mail API for reading mail as well. It makes it easy to switch protocols if need be (POP3, IMAP, Mbox, and Maildir). Only the IMAP and Maildir storage classes support setting flags at this time.

Zend Framework 也有用于阅读邮件的 Zend_Mail API。如果需要,它可以轻松切换协议(POP3、IMAP、Mbox 和 Maildir)。目前只有 IMAP 和 Maildir 存储类支持设置标志。

http://framework.zend.com/manual/en/zend.mail.read.html

http://framework.zend.com/manual/en/zend.mail.read.html

Read messages example from the Zend Framework docs:

阅读 Zend 框架文档中的消息示例:

$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";
}