2015-10-16 128 views
3

我想從我的託管服務器連接到gmail服務器會引發一個錯誤,稱爲無法連接到gmail-imap.l.google.com,993:連接拒絕,但是當我連接它時從本地主機,然後一切正常。使用PHP連接到IMAP

貝婁你可以找到我的代碼。

<?php 

/* Created on: 11/3/2008 By Ron Hickey 6tx.net/gmail_mod 
    Gmail mod for admin panels or you can edit it and convert html results to XML for personal RSS reader */ 

// enter gmail username below e.g.--> $m_username = "yourusername"; 
$m_username = "[email protected]"; 

// enter gmail password below e.g.--> $m_password = "yourpword"; 
$m_password = "password"; 

// enter the number of unread messages you want to display from mailbox or 
//enter 0 to display all unread messages e.g.--> $m_acs = 0; 
$m_acs = 15; 

// How far back in time do you want to search for unread messages - one month = 0 , two weeks = 1, one week = 2, three days = 3, 
// one day = 4, six hours = 5 or one hour = 6 e.g.--> $m_t = 6; 
$m_t = 2; 

//----------->Nothing More to edit below 
//open mailbox..........please 
$m_mail = imap_open("{imap.gmail.com:993/imap/ssl}INBOX", $m_username, $m_password) 

// or throw a freakin error............you pig 
     or die("ERROR: " . imap_last_error()); 

// unix time gone by or is it bye.....its certanly not bi.......or is it? ......I dunno fooker 
$m_gunixtp = array(2592000, 1209600, 604800, 259200, 86400, 21600, 3600); 

// Date to start search 
$m_gdmy = date('d-M-Y', time() - $m_gunixtp[$m_t]); 

//search mailbox for unread messages since $m_t date 
$m_search = imap_search($m_mail, 'UNSEEN SINCE ' . $m_gdmy . ''); 

//If mailbox is empty......Display "No New Messages", else........ You got mail....oh joy 

$m_empty = ""; 

if ($m_search < 1) { 
    $m_empty = "No New Messages"; 
} else { 

// Order results starting from newest message 
    rsort($m_search); 

//if m_acs > 0 then limit results 
    if ($m_acs > 0) { 
     array_splice($m_search, $m_acs); 
    } 

//loop it 
    foreach ($m_search as $what_ever) { 

//get imap header info for obj thang 
     $obj_thang = imap_headerinfo($m_mail, $what_ever); 

//Then spit it out below.........if you dont swallow 
     echo "<body bgcolor=D3D3D3><div align=center><br /><font face=Arial size=2 color=#800000>Message ID# " . $what_ever . "</font> 

<table bgcolor=#D3D3D3 width=700 border=1 bordercolor=#000000 cellpadding=0 cellspacing=0> 
<tr> 
<td><table width=100% border=0> 
<tr> 
<td><table width=100% border=0> 
<tr> 
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Date:</font> <font face=Arial size=2 color=#000000>" . date("F j, Y, g:i a", $obj_thang->udate) . "</font></td> 
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>From:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->fromaddress . "</font></td> 
<td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>To:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->toaddress . " </font></td> 
</tr> 
<tr> 
</table> 
</td> 
</tr><tr><td bgcolor=#F8F8FF><font face=Arial size=2 color=#800000>Subject:</font> <font face=Arial size=2 color=#000000>" . $obj_thang->Subject . "</font></td></tr><tr> 
</tr> 
</table></td> 
</tr> 
</table><a href=http://gmail.com target=_blank><font face=Arial size=2 color=#800000>Login to read message</a></font><br /></div></body>"; 
    } 
} echo "<div align=center><font face=Arial size=4 color=#800000><b>" . $m_empty . "</b></font></div>"; 
//close mailbox bi by bye 
imap_close($m_mail); 
+0

登錄到您的Gmail,您可以從谷歌郵件中發現他們阻止了未經授權的訪問。按照郵件中的說明進行訪問。 –

+1

當它在本地而不是從你的服務器工作時,可能還有端口限制(端口可能被託管服務器上的防火牆阻止)。 – Jan

+0

我檢查了你的代碼,在代碼/端口/配置中沒有問題。只有您需要允許從外部服務器訪問。 –

回答

0

我的服務器hostgator不允許我使用imap連接到Gmail服務器。因此,我要將我的業務遷移到其他地方。

0
$hostname = '{imap.googlemail.com:993/imap/ssl/novalidate-cert}INBOX'; 
    $server = '{imap.googlemail.com:993/imap/ssl/novalidate-cert}INBOX'; 
    $username = '[email protected]'; 
    $password = 'xxxxxxxxxxx'; 

    $inbox = imap_open($hostname,$username,$password,NULL,1) or die('Cannot connect to Mail Server: ' . print_r(imap_errors())); 
    /* grab emails */ 

    $emails = imap_search($inbox,'UNSEEN'); // UNSEEN = for unread mail,SEEN = READ mail , ALL = all mail READ as well as UNREAD mail 

我用上面的代碼與郵件服務器 連接嘗試

$收件箱= imap_open($主機,$的用戶名,$密碼,NULL,1)或死亡(「無法連接到郵件服務器:'。print_r(imap_errors()));