2014-02-17 41 views
0

重新訪問我的previous question中指定的這個問題,我嘗試過並嘗試過,也使用不同的帳戶(我試過gmail以及outlook),但問題仍然存在。我得到的錯誤是下面的,如果我嘗試,如果我嘗試在我的Outlook帳戶訪問電子郵件訪問我的谷歌帳戶CakePHP-使用IMAP訪問郵件

Error: Unable to get imap_thread after 4 retries. 'Can't open mailbox {imap.gmail.com:993/ssl/imap/tls/novalidate-cert}INBOX: invalid remote specification' 

,錯誤是一樣的:

Error: Unable to get imap_thread after 4 retries. 'Can't open mailbox {outlook.office365.com:993/ssl/imap/tls/novalidate-cert}INBOX: invalid remote specification' 

我的設置如下:

public $emailTicket = array(
     'datasource' => 'ImapSource', 
     'server' => 'outlook.office365.com', 
     'connect' => 'imap/tls/novalidate-cert', 
     'username' => 'my email here', 
     'password' => 'my password here', 
     'port' => '993', //incoming port 
     'ssl' => true, 
     'encoding' => 'UTF-8', 
     'error_handler' => 'php', 
     'auto_mark_as' => array(
     'Seen', 
     // 'Answered', 
     // 'Flagged', 
     // 'Deleted', 
     // 'Draft', 
     ), 
    ); 

我在本地機器上工作,有誰知道這是否可能是問題?有沒有人試過這個,爲他/她工作?我願意接受所有的投入!

我似乎無法找到這裏有什麼問題,我已經在這裏約2天了,所以如果任何人都可以幫助,我很感激! 另外這裏的鏈接,我使用的plugin,由尼古拉斯·拉米..

回答

0

您可以使用下面的實現代碼,以滿足您的要求:

public function generate_email_response_pdf() 
{ 
$this->layout = false; 
$this->autoRender = false; 

$username = EMP_SMTP_MAIL_FROM; 
    $password = EMP_SMTP_MAIL_PASSWORD; 

$imap = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', $username, $password); 
$emails = imap_search($imap, 'ALL'); 
if(!empty($emails)) 
{ 
    //put the newest emails on top 
     rsort($emails); 
    foreach($emails as $email_number) 
    { 
    $flag = 0; 
    $mail_data = array(); 
    $file_name = array(); 
    $output = array(); 
    $savefilename = null; 
    $filename = null; 

    $overview = imap_fetch_overview($imap, $email_number, 0); 
    //initialize the subject index with -000, considering not receving this will not be received in 
    //subject line of email 
    $output['subject'] = '-000x'; 
    if(isset($overview[0] -> subject)) 
    { 
     $output['subject'] = $overview[0] -> subject; 
     } 

    $structure = imap_fetchstructure($imap, $email_number); 
    if(property_exists($structure, 'parts')) 
    { 
     $flag = 1; 
     $flattened_parts = $this->flatten_parts($structure->parts); 
     foreach($flattened_parts as $part_number => $part) 
     { 
     switch($part->type) 
     { 
      case 0: 
      //the HTML or plain text part of the email 
      if((isset($part->subtype)=='HTML')&&(isset($part->disposition)=='ATTACHMENT')) 
      { 
       $part_number = 1.2; 
      } 
      else if(isset($part->subtype)=='HTML') 
      { 
       $part_number = $part_number; 
      } 
      else 
      { 
       $part_number = $part_number; 
      }    
      $message = $this->get_part($imap, $email_number, $part_number, $part->encoding); 
      //now do something with the message, e.g. render it 
      break; 

      case 1: 
      // multi-part headers, can ignore 
      break; 

      case 2: 
      // attached message headers, can ignore 
      break; 

      case 3: // application 
      case 4: // audio 
      case 5: // image 
      case 6: // video 
      case 7: // other 
      break; 
     } 
     if(isset($part->disposition)) 
     { 
      $filename = $this->get_filename_from_part($part); 
      if($filename) 
      { 
      // it's an attachment 
      $attachment = $this->get_part($imap, $email_number, $part_number, $part->encoding); 

      $file_info = pathinfo($filename); 
      $savefilename = RESPONSE_ATTACHMENT_PREFIX.$file_info['filename'].'_'.$this->_getRandId(4).'.'.$file_info['extension']; 
      $file_name[] = $savefilename; 

      $attachment_file_name = $this->save_attachment($attachment, $savefilename, $directory_path); 
      //imap_fetchbody($imap, $email_number, 2); //This marks message as read 
      } 
      else 
      { 
      // don't know what it is 
      } 
     } 
     } 
    } 
    else 
    { 
     $encoding = $structure->encoding; 
     $message = imap_fetchbody($imap, $email_number, 1.2); 
     //echo $message; die; 
     if($message == "") 
     { 
     $message = imap_body($imap, $email_number); 
     if($encoding == 3) 
     { 
      $message = base64_decode($message); 
     } 
     else if($encoding == 4) 
     { 
      $message = quoted_printable_decode($message); 
     } 
     } 
    }  
    $header = imap_headerinfo($imap, $email_number); 

    $from_email = $header->from[0]->mailbox."@".$header->from[0]->host; 
    $to_email = $header->to[0]->mailbox."@".$header->to[0]->host; 
    $reply_to_email = $header->reply_to[0]->mailbox."@".$header->reply_to[0]->host; 

    $cc_email = array(); 
    if(isset($header->cc)) 
    { 
     foreach($header->cc as $ccmail) 
     { 
     $cc_email[] = $ccmail->mailbox.'@'.$ccmail->host; 
     } 
     $cc_email = implode(", ", $cc_email); 
    } 
    $output['to'] = $to_email; 
    $output['from'] = $from_email; 
    $output['reply_to'] = $reply_to_email; 
    $output['cc'] = $cc_email; 

    $formatted_date = date('D, d M Y h:i A', strtotime($overview[0] -> date)); 
    $output['date'] = $formatted_date; 
    $output['message'] = $message; 
    $output['flag'] = $flag; 
    $mail_data['Attachment'] = $file_name; 
    $mail_data['Data'] = $output; 
    $this->set('response_data', $mail_data); 
    $mail_content = null; 
    if(!empty($mail_data)) 
    { 
     $this->viewPath = 'Elements/default'; 
     $mail_content = $this->render('pdf_content'); 
    } 
    $header = null; 
    $footer = null; 
    $html = preg_replace(array('/[^\r\n\t\x20-\x7E\xA0-\xFF]*/'), '', $mail_content); 
    $pdfFile = $this->_generateWkPdf($html, $directory_path, $new_file_name, $header, $footer); 

    $image_type = EXT_JPG; 
    $response_files_array = $this->_generateImagesFromPdf($directory_path.$pdfFile, $directory_path, $new_file_name, $image_type); 
    } 
} 
imap_close($imap); 
} 
+0

嗨阿倫感謝您的回覆!我怎樣才能在視圖中渲染這一點..我沒有註釋前兩行。我需要遍歷檢索的電子郵件,並顯示主題。然後,一旦點擊,電子郵件正文將顯示。謝謝 – LogixMaster

+0

要顯示多個電子郵件,首先必須使用$ output作爲二維數組,最後使用'$ this-> set('output',$ output)'。要在您的視圖中顯示它,請使用任何Jquery Accordion插件。 –