Create directories and write files using PHP from a sendmail pipe to program -


i have script reads emails (with attachments) pipe , i'm trying save attachment(s) disk further processing. i've cobbled code few sites , life of me cannot files save. i'm using 777 chmod value permissions don't seem problem wanted know if maybe i'm limited php commands when using command line processor rather browser. also, i've hardcoded "include" directories in event file not executed directory located. appreciated!

#!/usr/bin/php <?php //debug #ini_set ("display_errors", "1"); #error_reporting(e_all);  include('/var/www/simple_html_dom.php');  //include email parser require_once('/var/www/rfc822_addresses.php'); require_once('/var/www/mime_parser.php');  // read email in stdin $fd = fopen("php://stdin", "r"); $email = ""; while (!feof($fd)) {     $email .= fread($fd, 1024); } fclose($fd);  //create email parser class $mime=new mime_parser_class; $mime->ignore_syntax_errors = 1; $parameters=array(     'data'=>$email, );  $mime->decode($parameters, $decoded);  //---------------------- email header info -----------------------//  //get name , email of sender $fromname = $decoded[0]['extractedaddresses']['from:'][0]['name']; $fromemail = $decoded[0]['extractedaddresses']['from:'][0]['address'];  //get name , email of recipient $toemail = $decoded[0]['extractedaddresses']['to:'][0]['address']; $toname = $decoded[0]['extractedaddresses']['to:'][0]['name'];  //get subject $subject = $decoded[0]['headers']['subject:'];  $removechars = array('<','>');  //get message id $messageid = str_replace($removechars,'',$decoded[0]['headers']['message-id:']);  //get reply id //$replytoid = str_replace($removechars,'',$decoded[0]['headers']['in-reply-to:']);  //---------------------- find body -----------------------//  //get message body if(substr($decoded[0]['headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['body'])){      $body = $decoded[0]['body'];  } elseif(substr($decoded[0]['parts'][0]['headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['parts'][0]['body'])) {      $body = $decoded[0]['parts'][0]['body'];  } elseif(substr($decoded[0]['parts'][0]['parts'][0]['headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($decoded[0]['parts'][0]['parts'][0]['body'])) {      $body = $decoded[0]['parts'][0]['parts'][0]['body'];  }     $my_dir = base64_encode($fromemail);     shell_exec('mkdir -p /var/www/tmp/' . $my_dir . ' -m 777');     //mkdir($_server['document_root'] . "tmp/" . $my_dir, 0777, true);      $target_path = "var/www/tmp/" . $my_dir;     //chdir($target_path);      //------------------------ attachments ------------------------------------//  //loop through email parts foreach($decoded[0]['parts'] $part){      //check attachments     if($part['content-disposition'] == 'attachment'){          //format file name (change spaces underscore remove isn't letter, number or underscore)         $filename = preg_replace('/[^0-9,a-z,\.,_]*/i','',str_replace(' ','_', $part['filename']));          // write data file         $fp = fopen($target_path . "/" . $filename, 'w');         $written = fwrite($fp,$part['body']);         fclose($fp);          //add file attachments array         $attachments[] = $part['filename'];      }  }  $html = file_get_html($attachments); 

update: informative response...i've been trying figure out how run command line. i'm getting errors now, still don't make sense:

php notice:  undefined index: name in /var/www/catcher.php on line 38 php notice:  undefined index: content-disposition in /var/www/catcher.php on line 80 php notice:  undefined index: content-disposition in /var/www/catcher.php on line 80 php notice:  undefined variable: attachments in /var/www/catcher.php on line 97 php warning:  file_get_contents(): filename cannot empty in /var/www/simple_html_dom.php on line 39 

i have specified full include path other files , smmta user should have read access /var/www/ directory 755.

using 0777 permissions on mkdir command in script not matter @ if user script running under cannot write target directory. so, if script running used sendmail example, make sure user can write /var/www/tmp.

some debugging tips: complete e-mail , save file. figure out user script runs (e.g. sendmail). execute script manually commandline , watch errors. e.g:

sudo -u sendmail /path/to/script.php < /path/to/saved-email.eml 

make sure have error reporting turned on etc.

edit: looking @ errors posted, seems mime decoder cannot parse message way you're expecting it. appear not doing error checking, instead notices , warnings undefined indexes.

check in input , output of decoder. make sure message decoded way expect be.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -