我正在建立一個基於聊天/短信的系統我有它發出的消息,但我需要做的是實時更新傳入電子郵件中的數據,而無需用戶重新加載(阿賈克斯?)我需要從主頁面傳遞一個數字到fetch.php,它可以獲取電子郵件,並創建一個沒有被閱讀並且來自正確發送者的電子郵件的數組,我需要做的事情是將數字從主頁面發送到抓取頁面並返回一組新的消息到主textarea,但是我在ajax上找到的所有教程似乎都需要一個數據庫,並且我不知道如何在延遲幫助中運行和返回數據,我們將不勝感激。基於電子郵件的PHP聊天窗口
這裏是fetch.php的內容:
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$password = 'passwd';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'ALL');
/* if emails are returned, cycle through each... */
if($emails) {
$messages[] = '';
/* begin output var */
$output = '';
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number) {
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);
//print_r($overview);
$Is_sms = strpos($overview[0]->from, "txt.voice.google.com");
if($Is_sms === false) continue;
if($overview[0]->seen != 0) continue;
$pnl = strpos($overview[0]->from, ".");
$pnumber = substr($overview[0]->from, $pnl +2, 10);
if($pnumber != "3303331866") continue;
$messages[] = $message;
//$status = imap_setflag_full($mbox, $mail, "\\Seen \\Flagged", ST_UID);
/* output the email header information */
/*$output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">';
$output.= '<span class="subject">'.$overview[0]->subject.'</span> ';
$output.= '<span class="from">'.$overview[0]->from.'</span>';
//$output.= '<span class="date">on '.$overview[0]->date.'</span>';
$output.= '<span class="pnumber">'.$pnumber.'</span>';
$output.= '</div>';*/
/* output the email body */
//$output.= '<div class="body">'.$message.'</div>';
}
//echo $output;
print_r($messages);
}
/* close the connection */
imap_close($inbox);
主要就是頁面只是一個號文本,內容文本區域,消息文本框,併發送按鈕。