2017-12-21 1 views
1

imap 함수에 문제가 있습니다. 기본적으로 보이지 않는 메일을 읽어야합니다. 모든 메일에 URL이있을 것이므로 해당 URL을 가져 와서 저장해야합니다.IMAP 함수와 관련된 문제 php

$inbox = imap_open($hostname,$username,$password); 
if($inbox)//if **1 
{ 
/* grab emails */ 
$emails = imap_search($inbox,'UNSEEN'); 

/* if emails are returned, cycle through each... */ 
if($emails) //if **2 
{ 
    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    $varients=array("1","1.1","1.2","2"); 

    foreach($emails as $email_number) //for loop **1 
    { 
     $ids [] = $email_number; 

     foreach($varients as $cur_varient) //for loop **2 
     { 
      echo "\n\nstarting with imap function number ". $cur_varient."\n\n"; 

      $overview  = imap_fetch_overview($inbox,$email_number,0);//all varients of like subject, date etc. 
      $from   = addslashes(trim($overview[0]->from)); 
      $inboxed_time = addslashes(trim(strtotime($overview[0]->date))); 
      $message  = (imap_body($inbox,$email_number,$cur_varient)); 

      print addslashes(trim($overview[0]->subject));break; 
      preg_match_all('#\bhttp?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $message, $match); 

      $link_matched = $match[0];     
      $input  = 'unsubscribe.php'; 
      $linkexists = false; 

      foreach($link_matched as $curlink) 
      {     
       if(stripos($curlink, $input) !== false) 
       { 
       $linkexists = true; 
       $unsublink = $curlink; 

       $unsublink = str_replace('href="', '', $unsublink); 
       $unsublink = str_replace('"', '', $unsublink); 

       break; 
       } 
      } 



      if(isset($unsublink)) 
      { 
       $unsublink = addslashes(trim(($unsublink))); 
       $thread = 1; 
       $time  = date("Y-m-d H:i:s"); 
       $iQry  = " INSERT INTO `SPAMS`.url_queue VALUES("; 
        $iQry  .= " 'default','".$unsublink."','".$thread."','"; 
       $iQry  .= "".$from."','".$inboxed_time."',UNIX_TIMESTAMP('".$time."'))"; 

       //mysql_query($iQry); 
       print $iQry; 
      } 
     }//closing for loop **2 

    }//closing for loop **1 

} //closing if **2 

// Setting flag from un-seen email to seen on emails ID. 
if(isset($ids)) 
{ 
    imap_setflag_full($inbox,implode(",", $ids), "\\Seen \\Flagged"); //IMPORTANT 
} 

// colse the connection 
imap_expunge($inbox); 
imap_close($inbox); 
}//closing if **1 

다른 종류의 메일을 읽을 수 있도록 모든 다른 종류의 imap을 사용했습니다. 이제 문제는 일치하는 URL이 깨지는 경우입니다. 절반의 URL 만 가져올 것입니다 (전체 메시지를 인쇄 한 결과 절반의 URL이 다음 줄로 나옵니다). 다른 문제는 때로는 가져온 본문이 현재 메일에 포함되지 않는다는 것입니다. 다른 메일 내용을 가져 왔습니다.

나는 무엇을 해야할지 모르겠다. 그래서 내 전체 코드를 넣는 것이 도움이된다.

답변

0

여러 줄 문자를 일치 시키려면 정규 표현식을 사용해야하거나 전자 메일 본문에서 줄 바꿈 문자 등을 제거 할 수 있습니다.

preg_match("/{pattern}/mi",'Testing'); 
//m for multiline matches and i for case-insensitive matches 

두 번째 문제는 이메일이 여러 기관, 첨부 파일에 대한 간단한 텍스트 하나, HTML을위한 하나의 일부를 (그리고 그들의 순서는 애플 제품에서 다른)이, 당신이 생각하는 것보다 조금 다르다. https://www.electrictoolbox.com/php-imap-message-parts/

아마도 잘못된 것을 잡을 문제가 있습니다. 내 추천은 다음과 같이 모든 이메일 본문을 가져 오는 것입니다.

$overview = imap_fetch_overview($this->connection, $email_number, 0); 
$structure = imap_fetchstructure($this->connection, $email_number); 
$message = ""; 
//$parts = [1, 1.1, 1.2, 2]; 
if (!$structure->parts)//simple email 
    $message .= "Simple: ". imap_fetchbody($this->connection, $email_number, 0). "<br/>"; 
else { 
    foreach ($structure->parts as $partNumber=>$part){ 
      if ($partNumber != 0) 
       $message .= "Part ".$partNumber.": ". imap_fetchbody($this->connection, $email_number, $partNumber)."<br/>"; 
     } 
}