2013-01-02 2 views
0

내 머리 위에 매달려 작동하는 기한이 만료되어 문제가 발생합니다. 다음 코드는 이전 웹 서버에서 성공적으로 실행되었습니다. 그러나 아마존 EC2로 이동하면 호출 된 페이지가 그 페이지를 벗어나서 열리지 않습니다 (즉, 작동하지 않습니다). 나는 포트 993는 EC2에 열려 있는지 확인 만든하지만 서버 설정에 새로 온 사람으로서 내가 뭔가 누락 될 수 있습니다 :EC2에서 Gmail에 연결

function downloadData($emailAddress, $reportUrl, $reportType) 
{ 
    $result = "error"; 
    // DOWNLOAD DATA 
    // the max time allows for the email to download 
    set_time_limit(3000); 

    // connect to gmail with your credentials 
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
    $username = $emailAddress; # e.g [email protected] 
    $password = '*************'; 


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


    /* get all new emails. If set to 'ALL' instead 
    * of 'NEW' retrieves all the emails, but can be 
    * resource intensive, so the following variable, 
    * $max_emails, puts the limit on the number of emails downloaded. 
    * 
    */ 
    $emails = imap_search($inbox,'ALL'); 

    // useful only if the above search is set to 'ALL' 
    $max_emails = 5; 


    // if any emails found, iterate through each email 
    if($emails) 
     { 
      $count = 1; 

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

        // get mail message 
        $message = imap_fetchbody($inbox,$email_number,2); 

        // get mail structure 
        $structure = imap_fetchstructure($inbox, $email_number); 

        $attachments = array(); 

        // if any attachments found... 
        if(isset($structure->parts) && count($structure->parts)) 
         { 
          for($i = 0; $i < count($structure->parts); $i++) 
          { 
           $attachments[$i] = array(
            'is_attachment' => false, 
            'filename' => '', 
            'name' => '', 
            'attachment' => '' 
            ); 

          if($structure->parts[$i]->ifdparameters) 
           { 
            foreach($structure->parts[$i]->dparameters as $object) 
             { 
              if(strtolower($object->attribute) == 'filename') 
               { 
                $attachments[$i]['is_attachment'] = true; 
                $attachments[$i]['filename'] = $object->value; 
               } 
             } 
           } 

          if($structure->parts[$i]->ifparameters) 
           { 
            foreach($structure->parts[$i]->parameters as $object) 
             { 
              if(strtolower($object->attribute) == 'name') 
               { 
                $attachments[$i]['is_attachment'] = true; 
                $attachments[$i]['name'] = $object->value; 
               } 
             } 
           } 

          if($attachments[$i]['is_attachment']) 
           { 
            $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 

            // 4 = QUOTED-PRINTABLE encoding 
            if($structure->parts[$i]->encoding == 3) 
             { 
              $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
             } 
            // 3 = BASE64 encoding 
            elseif($structure->parts[$i]->encoding == 4) 
             { 
              $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
             } 
               //imap_delete($inbox,$email_number); 
               imap_mail_move($inbox, $email_number,'[Gmail]/Bin'); 
             } 
           } 
          } 

        // iterate through each attachment and save it 
        foreach($attachments as $attachment) 
         { 
          if($attachment['is_attachment'] == 1) 
           { 
            // SCAN REPORT SET OUTSIDE OF FUNCTION 
            $filename = $reportUrl; 
            if(empty($filename)) $filename = $attachment['filename']; 
            if(empty($filename)) $filename = time() . ".dat"; 

            /* prefix the email number to the filename in case two emails 
            * have the attachment with the same file name. 
            */ 
            $fp = fopen($filename, "w+"); 
            fwrite($fp, $attachment['attachment']); 
            fclose($fp); 
            $result = "success"; 
           } 

         } 
        if($count++ >= $max_emails) break; 
       } 
     } 
    // close the connection 
    imap_close($inbox,CL_EXPUNGE); 
    return $result; 
} 

오류 페이지 단지 곳에서로드되지 않는다 (예 : 최대 온다 불구을 함수가 호출 됨) 콘솔에서 다음 오류를 반환합니다. 500 (내부 서버 오류)

내가 누락 될 수있는 제안이 있으십니까?

편집 : 보인다 그 다음 코드 조각이 기능에 포함 된 경우에만 페이지 오류 : 문제가 내 EC2 보안 그룹에 따라 비록 포트가 있음을

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

    // close the connection 
    imap_close($inbox,CL_EXPUNGE); 

나에게 제안하는 올바른 포트 (993)가 열려 있습니다. 이런 과정에서 열려야하는 다른 포트가 있습니까?

+1

오류 메시지가 무엇입니까? – nnevala

+0

죄송합니다. 충분히 명확하지 않다고 생각합니다. 오류 메시지가 없습니다. 페이지에서 사용되는 함수가 아무 것도하지 않지만 호출 된 후 코드가 읽히지 않습니다. 따라서 함수가 사용되기 전에 코드에서만 절반로드 된 페이지가됩니다. . –

+0

콘솔에이 오류가 발생합니다 : 500 (내부 서버 오류) 함수가 호출 될 때만. –

답변

0

EC2가 VPC에 없는지 확인하십시오! EC2 인스턴스가 VPC 내에있을 때; NAT 인스턴스 등을 통해 인터넷에 연결하기위한 특별한 구성이 필요합니다.

"EC2 인스턴스에서 ssh"를 수행 한 다음 첫 번째 테스트에서 포트 993에서 Google에 액세스 할 수 있는지 확인하십시오. imap.google.com에서 ping하려고 시도 할 수 있습니다.

+0

그걸 가지고 뭔가있을 것 같습니다. imap.google.com으로 핑을 시도했지만 인스턴스에 할당 된 VPC ID가 없어도 'ping : unknown host imap.google.com'이 표시되었습니다. –

+0

EC2 인스턴스가 VPC 뒤에있는 것처럼 보입니다. EC2 세부 사항을 확인하십시오. VPC 뒤에 있으면 "VPC-ID"가 표시됩니다. 따라서,보다 쉬운 솔루션은 EC2 outisde VPC를 옮길 수 있다는 것입니다. 현재 EC2 인스턴스가 외부 인터넷에 연결되어 있지 않은 문제는 매우 기본적인 문제입니다 – Deepak

+0

인스턴스에 할당 된 VPC ID가 없습니다 –

관련 문제