【发布时间】:2015-11-27 11:15:24
【问题描述】:
我正在尝试借助 php 在我的网站上创建 IMAP 服务。它基本上只有几个步骤。
我想要的主要部分是我得到一个gmail帐户的文件夹列表,点击一个特定的文件夹,该文件夹中的邮件列表应该打开,点击任何特定的邮件,它的详细信息应该打开。
我有文件夹列表,我有邮件列表,我有邮件详细信息,但它们是独立的部分,但我希望组合并创建一个如上所述的流程。
下面给出3个步骤的代码
第 1 步 列出文件夹:它将列出gmail的所有文件夹。我的代码是
$folders = imap_list($imap, "{imap.gmail.com:993/imap/ssl}", "*");
echo "<ul>";
foreach ($folders as $folder) {
$folder = str_replace("{imap.gmail.com:993/imap/ssl}", "", $folder);
$folder = str_replace("[Gmail]/", "", $folder);
echo '<li>' . $folder . '</li>';
}
echo "</ul>";
第1步的o/p
INBOX
DRAFT
SENT
TRASH
第 2 步 列出电子邮件(单击文件夹邮件列表应该会打开)
$numMessages = imap_num_msg($imap);
for ($i = $numMessages; $i > ($numMessages - 20); $i--)
{
$header = imap_header($imap, $i);
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
$details = array(
"fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
? $fromInfo->mailbox . "@" . $fromInfo->host : "",
"fromName" => (isset($fromInfo->personal))
? $fromInfo->personal : "",
"replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
? $replyInfo->mailbox . "@" . $replyInfo->host : "",
"replyName" => (isset($replyTo->personal))
? $replyto->personal : "",
"subject" => (isset($header->subject))
? $header->subject : "",
"udate" => (isset($header->udate))
? $header->udate : ""
);
$uid = imap_uid($imap, $i);
$datee= gmdate("F j, Y, g:i a", $details["udate"] );
echo "<ul>";
echo "<li><strong>From:</strong>" . $details["fromName"];
echo " " . $details["fromAddr"] . "</li>";
echo "<li><strong>Subject:</strong> " . $details["subject"] . "</li>";
echo "<li><strong>DATE:</strong> " . $datee . "</li>";
}
步骤 2 的o/p(单击该邮件的特定邮件内容应该会打开)
From:ABC
Subject: TOPIC
DATE: September 2, 2015, 9:00 am
第 3 步 查看消息
$message_count = imap_num_msg($imap);
for ($i = 1; $i <= $message_count; ++$i) {
$header = imap_header($imap, $i);
$body = trim(substr(imap_body($imap, $i), 0, 100));
$prettydate = date("jS F Y", $header->udate);
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
echo "On $prettydate, $email said \"$body\".\n";
}
谁能告诉我如何达到上述要求
【问题讨论】:
-
请注意,在 Google 允许您通过 IMAP 访问电子邮件之前,您可能必须验证服务器的 IP。 (github.com/rapideinternet/php-validate-imap-ip-gmail)
标签: php email phpmyadmin imap gmail-imap