通过imap连接时gmail没有响应?

时间:2014-04-16 10:53:39

标签: php gmail imap gmail-imap

我写了一个简单的3行代码,用于通过imap-php连接到gmail。 我正在使用000webhost来运行它。 但是当我尝试打开它时,它会继续加载并在一段时间后显示"问题加载页面"或"没有收到数据",好像gmail服务器没有响应!! 有什么建议可以解决这个问题吗?

$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox";
$username = 'abc@gmail.com';
$password = "abc123";

$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: '    .   imap_last_error());

2 个答案:

答案 0 :(得分:3)

$server_name = "imap.gmail.com";
$account_name = "email@mydomain.com";
$account_pwd = "my_account_password"; 

$inbox = imap_open("{".$server_name.":993/imap/ssl/novalidate-cert}INBOX", 
$account_name, $account_pwd) OR error_imap_mail("Unable To Open Imap 
Connection","Imap Failure",imap_errors());

function error_imap_mail($subject,$messagetext,$errors) {
    global $report_email;
    $mailfrom = "support@mydomain.com";
    $mailsubject = "$subject";
    $mailbody = "$messagetext\n\n";
    if (count($errors) >0) {
        if (substr($errors[0],0,8) <> "[IN-USE]") {
            array_reverse($errors);
            foreach ($errors as $key=>$value) {
                $mailbody .= "\nErr No.".$key." Error:".$value;
            }
            foreach ($report_email as $value) {
                mail($value, $mailsubject, $mailbody, "From: $mailfrom");
            }
        }#if (substr($err_array[0],0,8) <> "[IN-USE]") {
    }#if (count($errors) >0) {
    exit;
}   

如果连接失败,请让您的脚本向您发送错误。

您是否在Gmail的帐户设置中启用了IMAP?

答案 1 :(得分:0)

试试这个:

$hostname = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
$username = 'abc@gmail.com';
$password = "abc123";

$inbox = imap_open($hostname,$username,$password)or die('Cannot connect to Gmail: ' .     imap_errors());

另请确保您通过GMail设置为您的帐户启用IMAP。