2017-12-27 17 views
0

ちょっと、私のスクリプトで電子メールの添付ファイルに拡張子csvがあるかどうか確認するその内容を配列の配列に追加し、次の電子メールに移動してから、すべての電子メールを調べたところで、配列の配列を別々のファイルに保存します。私が苦労しているのは、添付ファイルがどのファイルタイプであるかをチェックする部分であり、ファイルタイプがCSVであれば進行するだけです。オンラインでやるべきキーワードに敬意を払うようです。誰もが正しい方向に私を指すか、やって自分のコードを適応させることができれば、それは非常にそうHERESに私のコード今のところ をいただければ幸いですチェックして、任意の助けてくれてありがとう、あなたは事前に私を与えることができます。ファイルの種類がcsv(PHPを使用している場合のみ)のメールボックスを調べ、ファイルの内容に配列を設定する

<?php include (application.php); 
    /* connect to gmail with your credentials */ 
    $mailbox = '{imap.gmail.com:993/imap/ssl}INBOX'; /* This bit tells the script where to look and what folder to look for */ 
    $email_server_username = 'YOUR_email_server_username'; /* The email servers username */ 
    $email_server_username_server_password = 'YOUR_email_server_username_server_password'; /* The password asspciated with the above username */ 
    /* connect to sql db with your credentials */ 
    $username = ""; 
    $password = ""; 
    $databasename = "testdb"; /*Set testdb to the database name*/ 
    $databasename = new mysqli("localhost", $username, $password, $databasename) or die("Connection to server failed, please check email_server_username, password and database name"); /*local host should be the ip of the server unless this script is run locally*/ 
    set_time_limit(3000); 

    /* try to connect to server*/ 
    $inbox = imap_open($mailbox,$email_server_username,$email_server_username_server_password) or die('Failed to connect to Gmail: ' . imap_last_error()); 
    $email_server_usernames = imap_search($inbox, 'FROM "[email protected]"'); 
    /* if any email_server_usernames are found, the script will iterate through each of them */ 
    if($email_server_usernames) { 
     $count = 1; 
     /* sorts by newest first */ 
     rsort($email_server_usernames); 
     /* for every value in email_server_username... */ 
     foreach($email_server_usernames as $email_server_username_number){ 
      /* get information specific to this email */ 
      $overview = imap_fetch_overview($inbox,$email_server_username_number,0); 
      $message = imap_fetchbody($inbox,$email_server_username_number,2); 
      /* get mail structure */ 
      $structure = imap_fetchstructure($inbox, $email_server_username_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_server_username_number, $i+1); 
         /* Extracts the email contents into usable text, 3 = BASE64 encoding*/ 
         if($structure->parts[$i]->encoding == 3){ 
          $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
         } 
         /* 4 = QUOTED-PRINTABLE encoding */ 
         elseif($structure->parts[$i]->encoding == 4){ 
          $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 
         } 
        } 
       } 
      } 
      /* iterate through each attachment and save it */ 
      foreach($attachments as $attachment){ 
       if($attachment['is_attachment'] == 1) 
       { 
        $filename = $attachment['name']; 
        if(empty($filename)) $filename = $attachment['filename']; 

        if(empty($filename)) $filename = time() . ".dat"; 
        $folder = "attachment"; 
        if(!is_dir($folder)){ 
         mkdir($folder); 
        } 
        $fp = fopen("./". $folder ."/". $email_server_username_number . "-" . $filename, "w+"); 
        fwrite($fp, $attachment['attachment']); 
        fclose($fp); 
       } 
      } 
     } 
    } 
    /* close the connection to the email_server_username server */ 
    imap_close($inbox); 

    echo "####################Script ran with no errors####################"; 
?> 

答えて

0
if($attachments[$i]["file"]["type"] == "text/csv") 
{Result} 

これはあなたを助けるでしょう。

関連する問題