Trying To Write Script For MMS E-mail To PHP MySQL DB Parser

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Trying To Write Script For MMS E-mail To PHP MySQL DB Parser

    Hello All,

    I'm trying to write a script in PHP 4 or 5 (doesn't matter as long as its compatible with both) that will run via a Cron Job on the server to parse e-mails for MMS videos or Photos then upload to a given folder like /uploads and Store the email or information from the email to the mysql db at the same time as storing the files to the /upload dir, then it deletes the email once info is stored in db/file, i'm trying to re-create the same type of engine that Photobucket has. Any ideas where to start? or if there is a script someone has or if someone can point me in that direction?

    Thanks in Advance,
    -Allen

    #2
    hey mate just checked out some sites on google this 1 seems like its the best

    <?php
    $nl="<br/>\n";

    $site=array();
    $site['email_address'] = "content@somewhere.co.uk";
    $site['email_name'] = "Content from Somewhere";
    $site['pop_host'] = "pop.somewhere.co.uk";
    $site['host'] = "auth.smtp.somewhere.co.uk";
    $site['username'] = "content@somewhere.co.uk";
    $site['password'] = "password";
    $site['authentication'] = TRUE;
    $site['folder'] = "../safe/";

    $reader=new email_reader_class();
    $reader->set_account($site['pop_host'], $site['username'], $site['password']);
    $ok=$reader->connect();
    if($ok) // if connected
    {
    $count=$reader->get_count();
    if($report) echo $count.' emails on server<br/>';
    if($count>0) // if emails to process
    {
    $other_emails=TRUE;
    while($other_emails) // step through emails
    {
    $processed=FALSE;
    $head=$reader->get_header();
    $from_address = trim($head['from'][0]['mailbox']) . "@" . trim($head['from'][0]['host']);
    if($report) echo 'From: '.$from_address.'<br/>';
    if(eregi("@somewhere.co.uk", $from_address))
    {
    $message_id="";
    if(isset($head['message_id'])) $message_id=$head['message_id'];
    if($report) echo 'ID: '.$message_id.'<br/>';
    if(!empty($message_id))
    {
    $sql="SELECT * FROM email_processing WHERE message_id='".addslashes($message_id)."'";
    $result=run_mysql_query($sql);
    if(mysql_num_rows($result)==0) // if email hasnt been processed already
    {
    if($report) echo 'Not previously processed<br/>';
    $body=trim($reader->get_body());
    $attachments=$reader->get_attachments();
    while(list($key,$value)=each($attachments))
    {
    if(is_dir($site['folder']))
    {
    $filename=$site['folder'].$value['filename'];
    $filehandle=fopen($filename, "ab");
    fwrite($filehandle, $value['filedata']);
    fclose($filehandle);
    }
    }
    }
    }
    if($processed)
    {
    $other_emails=$reader->delete_current_email(); // <- when live
    }
    else
    {
    $other_emails=$reader->next_email(); // <- when testing
    }
    }
    else
    {
    $other_emails=$reader->next_email();
    }
    }
    }
    else
    {
    echo "No emails to process".$nl;
    }
    $reader->disconnect();
    }
    else
    {
    echo $reader->error;
    echo $nl;
    }

    ?>
    Last edited by Loony; 17.02.11, 04:48.
    Creator of
    Epix.Mobi

    Keep an Eye on us Big things coming soon!!!!
    Need something for your site hit me up here

    http://coding-talk.com/forum/main-fo...r-your-wapsite

    Comment


      #3
      Hey Loony,

      Thanks for that script idea, but I don't think it would work for me, I can show you a sample script i've been testing, but it doesn't work consistently. Sometimes some emails are read 2 or 3 times, some are skipped it just doesn't work consistently... ill post it below:

      Here is the include:

      Code:
      <?php
      /**************************************************************
      MMS/PHOTOBLOG CLASS
      Version 1.7
      
      Released 17 Dec 2005
      
      by ruben @ bolink.org
      http://www.bolink.org
      
      **************************************************************/
      // config!
      $pop3_address = "mms@allen.dj";
      $pop3_server = "localhost";
      $password = "***********";
      $images_dir = "./imgtst/";
      $date_format = "d-m-Y";
      
      $db_host = "localhost";
      $db_user = "testmms";
      $db_password = "*************";
      $db_name = "testmms";
      $db_table = "mms";
      
      // connecting to db
      @mysql_connect($db_host, $db_user, $db_password) or header("location: $url?error=1");
      @mysql_select_db($db_name) or header("location: $url?error=1");
      
      // check if table exist
      $url = "http://bolink.org/projects/mms/error.php"; // for error handling
      $check = mysql_query("SELECT * FROM `$db_table` LIMIT 0,1");
      if (!$check) {
          $query = "CREATE TABLE `$db_table` (id int(11) NOT NULL auto_increment,type text NOT NULL,subject text NOT NULL,toaddress text NOT NULL,fromaddress text NOT NULL,date text NOT NULL,UNIQUE KEY id (id)) TYPE=MyISAM; ";
          
          $result = mysql_query($query) or header("location: $url?error=1");
      }
      
      // check if directory is writable
      if (!is_writable($images_dir)) {
          @mkdir("$images_dir") or header("location: $url?error=2");
          @chmod($images_dir, 0777) or header("location: $url?error=3");
      }
      
      // check if imap works
      if (!function_exists('imap_open')) {
          header("location: $url?error=7");
      }
      
      // open mailbox and get all emails from server
      if (! $mail = @imap_open("{".$pop3_server.":143/imap/notls}INBOX", "$pop3_address", "$password")) {
          header("location: $url?error=4");
      }
      $headerstrings = imap_headers($mail);
      foreach($headerstrings as $headerstring) {
          preg_match("/[0-9]/", $headerstring, $number);
          // parse message
          $header = imap_fetchheader($mail, $number[0]);
          preg_match("/Date: (.*)?[\+|-]/", $header, $date);
          $date = htmlentities($date[1]);
          $imap = imap_fetchstructure($mail, $number[0]);
          
          if (! empty($imap->parts)) {
              for ($i = 1, $j = count($imap->parts); $i < $j; $i++) {
                  $msg = imap_fetchbody($mail, $number[0], $i + 1);
                  $part = $imap->parts[$i];
                  
      						
                  $headers = imap_header($mail, $number[0]);
                  $from = $headers->from; 
      			      $addr = "".$from[0]->mailbox."@".$from[0]->host.""; 
                  $subj = $headers->subject;
      						$to   = $headers->toaddress;
                  $date = date($date_format, strtotime(substr($headers->Date, 0, 22)));
                  $subj = preg_replace('/^.*Q\?(.*?)\?=$/', '\\1', $subj);
                  $subj = str_replace("_", " ", $subj);
                  $subj = str_replace("=2E", ".", $subj);
                  $subj = addslashes($subj);
                  
                  if ($part->type == "3") {
                      $ext = "3gp";
                  } else if ($part->type == "5") {
                      $ext = "jpg";
                  }
                  
                  $query = "INSERT INTO `$db_table` VALUES ('NULL','$ext','$subj','$to','$addr','$date')";
                  $result = mysql_query($query) or header("location $url?error=5");
                  $count = mysql_insert_id();
                  
                  if (! $handle = fopen("$images_dir/".$count.".$ext", "w")) {
                      header("location: $url?error=6");
                  }
                  fwrite($handle, imap_base64($msg));
                  fclose($handle);
              }
          }
      }
      imap_delete($mail, $number[0]);
      imap_expunge($mail);
      imap_close($mail);
      
      ?>
      Code:
      <?
      include("mms.class.php");
      ?>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title>MMS TEST BY: DJ ALLEN</title>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <style type="text/css">
      body {  
       color: #444;
      }
      td { border: 2px solid gray; }
      table {  font: 10px Verdana, sans-serif; border: 0px solid black; }
      a { text-decoration: none; color: white; }
      </style>
      </head>
      <body>
      <table>
      <tr><td colspan="2" align="center"><h1>Photo MMS Test By: DJ ALLEN PRODUCTIONS<br>
      Send Your Pic To: [email]mms@allen.dj[/email]</h1></td></tr>
      <?
      $query = "SELECT * FROM `$db_table` order by id DESC limit 50";
      $result = mysql_query($query) or die(mysql_error());
      while ($mms = mysql_fetch_array($result, MYSQL_ASSOC)) {
      if ($mms[type] == "3gp") {
      echo "<tr><td colspan=\"2\"><a href=\"$images_dir$mms[id].3gp\" title=\"click to zoom\">";
      echo "<object classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" width=\"320\" height=\"200\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\">";
      echo "<param name=\"SRC\" value=\"$images_dir$mms[id].3gp\">";
      echo "<param name=\"AUTOPLAY\" value=\"false\">";
      echo "<param name=\"type\" value=\"video/quicktime\">";
      echo "<param name=\"CONTROLLER\" value=\"true\">";
      echo "<embed src=\"$images_dir$mms[id].3gp\" width=\"300\" height=\"200\" autoplay=\"false\" controller=\"true\" type=\"video/quicktime\" pluginspage=\"http://www.apple.com/quicktime/download/\">";
      echo "</object>";
      echo "</a></td></tr><tr>";
      } else {
      echo "<tr><td colspan=\"2\"><a href=\"$images_dir$mms[id].jpg\" title=\"click to zoom\">";
      echo "<img alt=\"click to zoom\" width=\"300\" src=\"$images_dir$mms[id].jpg\"></a></td></tr><tr>";
      }
      echo "<td width=\"100\">text:</td>";
      echo "<td>$mms[subject]</td></tr><tr>";
      echo "<td>date:</td>";
      echo "<td>$mms[date]</td></tr><tr>";
      echo "<td>from:</td>";
      echo "<td>$mms[fromaddress]</td></tr><tr>";
      echo "<td>to:</td>";
      echo "<td>$mms[toaddress]</td></tr>";
      }
      ?>
      </table>
      
      </body>
      </html>
      Any ideas as to why that might be happening? Duplicate or Triple emails? Some not coming through? Some of the attachments not coming through? It's weird that it does it...

      -Allen

      Comment

      Working...
      X