The following code is not my own, and any attempt to contact the original author has proven to be fruitless.
Basically what I am trying to do is copy info from IMAP email to a MySQL database. I can copy the email info fine, except copying the attachment does not work!
This first bit is my own code:
Code: Select all
<?php
require('connection.php');
//open connection
$mbox = imap_open($mailserver, $mailuser, $mailpass);
//check the connection, or break if error
if(!imap_ping($mbox)){
//dropped off!
break;
exit();
}
//check number of messages
$inf = imap_check($mbox);
$many = $inf->Nmsgs;
//prep attachments function
require_once('attachment.read.php');
$savedirpath="attachments/";
foreach(range(1, $many) as $msno) {
$msg = imap_headerinfo($mbox, $msno);
$body = imap_body($mbox, $msno);
$subject = $msg->subject;
$fromaddress = $msg->fromaddress;
$datetime = $msg->date;
$subject = mysql_real_escape_string($subject, $db);
$body = mysql_real_escape_string($body, $db);
$query = "INSERT INTO emails (ID, subject, body, datetime, from) VALUES ('', '$subject', '$body', '$datetime', '$fromaddress')";
mysql_query($query, $db) or die(mysql_error($db));
//attachments
$jk=new readattachment();
$jk->getdata($mailserver,$mailuser,$mailpass,$savedirpath,$msno);
$row = mysql_fetch_assoc(mysql_query("SELECT ID FROM emails WHERE datetime='$datetime'", $db))or die(mysql_error($db));
$filename = mysql_real_escape_string($filename, $db);
mysql_query("INSERT INTO attachments (attachmentID, filename, emailID) VALUES ('', '" . $filename . "', '" . $row['ID'] . "')") or die(mysql_error($db));
//mark email for deletion
imap_delete($mbox, $msno);
}
//delete items marked for deletion
imap_expunge($mobx);
//close IMAP stream
imap_close($mbox);
?>Code: Select all
<?php
class readattachment {
function getdecodevalue($message,$coding){
if ($coding == 0) {
$message = imap_8bit($message);
} elseif ($coding == 1) {
$message = imap_8bit($message);
} elseif ($coding == 2) {
$message = imap_binary($message);
} elseif ($coding == 3) {
$message=imap_base64($message);
} elseif ($coding == 4) {
$message = imap_qprint($message);
} elseif ($coding == 5) {
$message = imap_base64($message);
}
return $message;
}
function getdata($mailserver,$mailuser,$mailpass,$savedirpath,$msno){
$mbox = imap_open ($mailserver, $mailuser, $mailpass) or die("can't connect: " . imap_last_error());
$message = array();
$message["attachment"]["type"][0] = "text";
$message["attachment"]["type"][1] = "multipart";
$message["attachment"]["type"][2] = "message";
$message["attachment"]["type"][3] = "application";
$message["attachment"]["type"][4] = "audio";
$message["attachment"]["type"][5] = "image";
$message["attachment"]["type"][6] = "video";
$message["attachment"]["type"][7] = "other";
$structure = imap_fetchstructure($mbox,$msno);
$parts = $structure->parts;
$fpos=2;
for($i = 1; $i < count($parts); $i++) {
$message["pid"][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT") {
$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
$message["subtype"][$i] = strtolower($part->subtype);
$ext=$part->subtype;
$params = $part->dparameters;
$filename=$part->dparameters[0]->value;
$mege="";
$data="";
$mege = imap_fetchbody($mbox,$msno,$fpos);
$filename="$filename";
$fp=fopen($filename,w);
$data=$this->getdecodevalue($mege,$part->type);
fputs($fp,$data);
fclose($fp);
$fpos+=1;
return $filename;
}
}
imap_close($mbox);
}
}
?>[text]Notice: Undefined property: stdClass::$disposition in C:\wamp\www\attachmentread.class.php on line 37[/text]
which obviously refers to
Code: Select all
if($part->disposition == "ATTACHMENT") {Any help would be appreciated guys