2017-12-21 8 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が次の行に来ることがわかりました)。もう1つの問題は、時々、取り込まれたボディが現在のメールに含まれていないことです。他のメールの内容を取得しました。

私は何をするのか困惑しているので、私のコード全体を入れてください。助けてください。

答えて

0

複数行のテキストにもマッチする正規表現修飾子を使用するか、電子メールの本文から改行などを取り除く必要があります。

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

あなたの第二の問題は、あなたが思うだろうよりも少し異なっている、電子メールは、簡単なテキストの1、htmlのための1、添付ファイルの一部を複数のボディを持っている(とその順序は、アップル製品に異なります)。 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/>"; 
     } 
} 
関連する問題