PHP Pipe to move email attachmnet to FTP folder

Hi All


I am looking for a php pipe script to take an attachment from a mail and send it to a specific ftp folder all help will be appreciated .

Thank you
TomCatELAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eddie ShipmanAll-around developerCommented:
Here's what we use to take attachments and insert a record in a table and then move the message to another mailbox.
import_mail.php
<?php

function showProgressMsg($msg)
{
  echo $msg."<br>";
  ob_flush();
  flush();
  usleep(2500);
}
  $mailpass="mail_password";
  $mailhost="{localhost/imap/ssl/novalidate-cert}";
  $mailuser="address@myhost.com";
  // this is an include with mySQL functions
  include "libs/database/db_mysql.php";
  $db = new Database;

  $debug=false;
  if($_GET['debug'] == 'yes') {
    $debug = true;
    ini_set('output_buffering', 'Off');
    ob_start();
  }
  
  if($debug) {showProgressMsg("Stage 1: Mem usage is: ".memory_get_usage());}
  if($debug) {showProgressMsg('Opening Mailbox');}
  
  $mailbox=imap_open($mailhost,$mailuser,$mailpass) or die("<br />\nFAILLED! ".imap_last_error());

  if($debug) {showProgressMsg("Stage 2: Mem usage is: ".memory_get_usage());}
  // The IMAP.xml file contains the email address and user_id of the users that we accept 
  // their files via email 
  if($debug) {showProgressMsg('Reading IMAP.xml');}
  // I open this XML file that contains the email addresses of people that we want
  // to parse their attachments.
  $xml = simplexml_load_string(file_get_contents('IMAP.xml'));
  $result = $xml->xpath('item');
  if($debug) {showProgressMsg("Stage 3: Mem usage is: ".memory_get_usage());}
  while(list( , $node) = each($result)) {
    $email   = $node->LI_email;
    $user_id = $node->LI_user_id;
    $search  = "UNSEEN FROM \"$email\"";

    if($debug) {showProgressMsg('Searching for '.$email);}
    $result2 = imap_search($mailbox, $search);
    if($result2) {
      $index = $result2[0];
      $structure = imap_fetchstructure($mailbox, $index);
      $ov = imap_fetch_overview($mailbox, $index);
      foreach ($ov as $overview) {
        $md = date_parse($overview->date);
        $md = $md['month'].'_'.$md['day'].'_'.$md['year'];
        if($debug) {showProgressMsg("Message Date: ".$md);}
      }
      $attachments = array();
      if(isset($structure->parts) && count($structure->parts)) {
        if($debug) {showProgressMsg('Handling attachments');}
        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($mailbox, $index, $i+1, FT_PEEK);
            if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
              $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
              if($debug) {showProgressMsg('Encoding '.$structure->parts[$i]->encoding);}
            }
            elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
              $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
            }
          }             
        } // for($i = 0; $i < count($structure->parts); $i++)
      } // if(isset($structure->parts) && count($structure->parts))

      $append = 'N';
      $update_qtys = 'N';
      // Now add a record into the file_upload table
      $filesToAppend=array('N','N','Y', 'Y', 'Y'); // this array contains for each file (starting with "0", just like $i).
      // Now add a record into the file_upload table
      for($i = 0; $i < count($attachments); $i++) {
        if (strlen(trim($attachments[$i]['filename'])) > 0) {
          $path_parts = pathinfo($attachments[$i]['filename']);
          if($debug) {showProgressMsg('Processing '.$attachments[$i]['filename']);}
          if(strtolower($path_parts['extension']) == 'zip') {
            // I am going to do something different with ziped files            
            $filename = 'file_uploads/temp/'.$user_id.'_'.$path_parts['filename'].'_'.$md.'.'.$path_parts['extension'];
            $fp = fopen($filename, "w");
            fwrite($fp, $attachments[$i]['attachment']);
            fclose($fp);                       
            $zip = new ZipArchive();
            if ($zip->open($filename) !== TRUE) {
              die ('Could not open archive');
            }
            $zippedfile = $zip->statIndex(0);
            $path_parts = pathinfo($zippedfile['name']);
            $newfilename = 'file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.$md.'.'.$path_parts['extension'];
            // Extract the zipped attachment to the file_uploads directory
            $zip->extractTo('file_uploads/', $zippedfile['name']);
            $zip->close();           
            unlink($filename);
            rename('file_uploads/'.$zippedfile['name'], $newfilename);
            if(!strtolower($path_parts['extension']) == 'xls' || !strtolower($path_parts['extension']) == 'xlsx') {
              $filestr = file_get_contents($newfilename);
            } else {
              $filestr = preg_replace('`[\r\n]+`',"\n", file_get_contents($newfilename));
            }
            $fp = fopen($newfilename, "w+");
            fwrite($fp, $filestr);
            fclose($fp);                       
          } else {
            $filename = 'file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.$md.'.'.$path_parts['extension'];
            $fp = fopen($filename, "w");
            if(!strtolower($path_parts['extension']) == 'xls' || !strtolower($path_parts['extension']) == 'xlsx') {
              $attachments[$i]['attachment'] = preg_replace('`[\r\n]+`',"\n",$attachments[$i]['attachment']);
            }
            fwrite($fp, $attachments[$i]['attachment']);
            fclose($fp);
          }
          if($debug) {showProgressMsg("Stage 4: Mem usage is: ".memory_get_usage());}
          // remove the directory from the filename
          $extracted['file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.$md.'.'.$path_parts['extension']] = $user_id;
        } // if (strlen(trim($attachments['name'])) > 0
      } // for($i = 0; $i < count($attachments); $i++)
      // This is a stop gap to circumvent the message being processed twice
      if($debug) {showProgressMsg('Setting \Seen Flag');}
      imap_setflag_full($mailbox, "$index", "\Seen");
      if($debug) {showProgressMsg('Moving Message from:'.$email);}
      // Now move the message to completed uploads mailbox
      // imap_mail_move copies the message and then sets the deleted flag.
      imap_mail_move($mailbox, "$index", "INBOX.completed uploads") or die("can't move: ".imap_last_error()); 
    } // if($result2)
  } // while(list( , $node) = each($result))
  if($debug) {showProgressMsg("Stage 5: Mem usage is: ".memory_get_usage());}
  imap_expunge($mailbox);
  imap_close($mailbox);  
  unset($attachments);
  unset($structure);
  unset($zip);
  if($debug) {showProgressMsg("Stage 6: Mem usage is: ".memory_get_usage());}
  // Now, process extract4ed files
  $append = 'N';
  $update_qtys = 'N';
  // Now add a record into the file_upload table
  $filesToAppend=array('N','N','Y', 'Y', 'Y'); // this array contains for each file (starting with "0", just like $i).
  $i = 0;

  /***********************************
    Sort the array here 
  ************************************/
  
  $one_inserted = false;
  if(count($extracted)>0) {
    foreach($extracted as $file=>$user_id) {
      $process = '0';
      // need to convert to tab-delimited format
      $path_parts = pathinfo($file);
      $filename = $path_parts['filename'].'.'.$path_parts['extension'];
      // Now insert into file_upload table
      $insert_sql = "INSERT INTO `file_upload` VALUES(NULL,$user_id, '$filename', {$process}, NOW(), '{$filesToAppend[$i]}', '{$update_qtys}')";
      if($debug) {showProgressMsg('Here\'s the SQL '.$insert_sql);}
      $i++;
      $db->query($insert_sql) or die("Can't insert record:".mysql_error());
    } // foreach($extracted as $file=>$user_id)
  } // if(count($extracted)>0)

  if($debug) {showProgressMsg('clearing out moved messages and Closing mailbox');}
  // This clears out the deleted messages
  ob_flush();
  flush();
  ob_end_clean();
  ini_set('output_buffering', 4096);
?>

Open in new window


We call this in a cron job every 15 minutes:
wget -a /home/my_user/public_html/file_uploads/import_logs/importlog.txt http://www.myhost.com/import_mail.php -O /dev/null

Open in new window

0
Ray PaseurCommented:
Here is my teaching example of how to run an email pipe.  I have not dealt with attachments, but I know this approach works in LAMP environments with cPanel.  At lines 55-60 it will capture the contents of whatever came into the pipe script and you can email that to yourself on line 63.  The email concept is useful because this script is started asynchronously upon arrival of each email, so it does not have any browser output.  Since you need to see output when you are debugging, you will be able to find it in your in-box.

HTH, ~Ray
#!/usr/bin/php -q
<?php // /email_pipe/index.php


// THIS IS AN EMAIL PIPE SCRIPT.
// THIS SCRIPT IS STARTED AUTOMATICALLY FOR EACH MESSAGE.
// NOTE THAT THIS SCRIPT IS ABOVE THE /public_html/ DIRECTORY TO PREVENT ACCIDENTAL EXECUTION


// --> HOW DO WE KNOW WHICH EMAIL MESSAGES GET SENT HERE?
// THIS SCRIPT RECEIVES MESSAGES SENT TO email_pipe@your.org
// CREATE AN EMAIL MAILBOX EXCLUSIVELY FOR AUTOMATED PROCESSING.
// SET UP AN EMAIL FORWARD FOR THAT MAILBOX IN cPANEL->EMAIL LIKE THIS:
// 1...5...10...15...20...25...
// |/home/{account}/email_pipe/index.php


// --> WHEN YOU UPLOAD, THIS SCRIPT WILL BE MARKED RW-R-R BUT THAT IS WRONG
// THIS SCRIPT MUST BE MARKED EXECUTABLE x0755
// YOU CAN USE FTP SOFTWARE TO CHMOD TO RWX-RX-RX


// --> NOTE THE FIRST LINE OF THIS SCRIPT MUST SAY #!/usr/bin/php -q STARTING IN COLUMN ONE
// 1...5...10...15...20...25...
// #!/usr/bin/php -q
// <?php ... PROGRAM CODE FOLLOWS


error_reporting(E_ALL);

// USE THE OUTPUT BUFFER - THIS DOES NOT HAVE BROWSER OUTPUT
ob_start();

// COLLECT THE INFORMATION HERE
$raw_email = '';


// TRY TO READ THE EMAIL FROM STDIN
if (!$stdin = fopen("php://stdin", "R"))
{
    echo "ERROR: UNABLE TO OPEN php://stdin \n";
}

// ABLE TO READ THE MAIL
else
{
    while (!feof($stdin))
    {
        $raw_email .= fread($stdin, 4096);
    }
    fclose($stdin);
}


// REMOVE MULTIPLE BLANKS - AND OTHER PROCESSING AS MIGHT BE NEEDED
$raw_email = preg_replace('/ +/', ' ', $raw_email);

// SPEW WHAT WE GOT, IF ANYTHING, INTO THE OUTPUT BUFFER
var_dump($raw_email);

// CAPTURE THE OUTPUT BUFFER AND SEND IT TO SOMEONE ELSE VIA EMAIL
$buf = ob_get_contents();
mail ('you@your.org', 'EMAIL PIPE DATA', $buf);
ob_end_clean();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
By reading the raw email, I am not sure if you can get the attachments without some heavy parsing.
Using the IMAP functions like in my example is usually the best way to do it.
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

TomCatELAuthor Commented:
Thanks for comments will test and give feedback
0
Eddie ShipmanAll-around developerCommented:
I guess you could also use Ray's pipe code and then add the code from this site to parse the incoming raw email data for attachments:
http://code.google.com/p/php-mime-mail-parser/
0
Eddie ShipmanAll-around developerCommented:
0
TomCatELAuthor Commented:
Sorry wanted to share solution points between Eddie and Ray don't know how to revert back .


I am going to try the solutions . Thank you
0
Eddie ShipmanAll-around developerCommented:
I will ask one of the moderators to modify the points.
0
TomCatELAuthor Commented:
Thank you
0
Eddie ShipmanAll-around developerCommented:
Just FYI, to reopen for point assignment, use the Request attention link.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.