redbrad0 Posted December 20, 2007 Share Posted December 20, 2007 I am connecting into my email via IMAP and downloading the messages to a mysql database and storing the attachments on a server. This all works fine with images, but if I get a .wav attachment then it does not save correctly for me to open back up at a later time. I thought this might of been a problem with using imap_base64 but it happens even when i remove this code. I have removed a bunch of code to make it easier for someone to see what is going on. I am trying to access the save attachment by just going to http://www.domain.com/_files/contact_us/filename.wav $SaveFileDirectory = "/home/domain/public_html/_files/contact_us/"; //check for new messages $mailbox = imap_open("{localhost:143/notls}INBOX", "email@address.com", "password") or die("Can't connect: " . imap_last_error());; // Check messages $check = imap_check($mailbox); if ($check->Nmsgs=='0') { echo "There are no messages that need to be downloaded"; } else { $emailIndex = 1; echo "Processing email " . $emailIndex . "<br>"; $header = imap_header($mailbox, $emailIndex); print("<PRE>"); print("Header Date : " . $header->Date . "<BR>"); print("Header To : " . $header->to) . "<BR>"; print("Header From : " . $header->from . "<BR>"); print("Header cc : " . $header->cc . "<BR>"); print("Header ReplyTo : " . $header->ReplyTo . "<BR>"); print("Header Subject : " . $header->Subject . "<BR></PRE>"); /* Lets find the email body */ $struct = imap_fetchstructure($mailbox, $emailIndex); $parts = $struct->parts; $i = 0; if (!$parts) { /* Simple message, only 1 piece */ $attachment = array(); /* No attachments */ $Email_Comments = imap_body($mailbox, $emailIndex); } else { /* Complicated message, multiple parts */ $endwhile = false; $stack = array(); /* Stack while parsing message */ $Email_Comments = ""; /* Content of message */ $attachment = array(); /* Attachments */ while (!$endwhile) { if (!$parts[$i]) { if (count($stack) > 0) { $parts = $stack[count($stack)-1]["p"]; $i = $stack[count($stack)-1]["i"] + 1; array_pop($stack); } else { $endwhile = true; } } if (!$endwhile) { /* Create message part first (example '1.2.3') */ $partstring = ""; foreach ($stack as $s) { $partstring .= ($s["i"]+1) . "."; } $partstring .= ($i+1); if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") { /* Attachment */ $attachment[] = array("filename" => $parts[$i]->parameters[0]->value, "filedata" => imap_fetchbody($mailbox, $emailIndex, $partstring)); } elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { /* Message */ $Email_Comments .= imap_fetchbody($mailbox, $emailIndex, $partstring); } } if ($parts[$i]->parts) { $stack[] = array("p" => $parts, "i" => $i); $parts = $parts[$i]->parts; $i = 0; } else { $i++; } } /* while */ } /* complicated message */ foreach ($attachment as $attach) { WriteToFile($SaveFileDirectory . $attach['filename'], imap_base64($attach['filedata'])); echo "Attachment found:" . $attach['filename'] . "<br>"; } } die; Quote Link to comment https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/ Share on other sites More sharing options...
ballouta Posted May 23, 2008 Share Posted May 23, 2008 Hello guys i used this code after eiditing some parts, but the function writetofile is undefined as the output says. would you please write for me the details of this fucntion: writetofile thanks Quote Link to comment https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/#findComment-548254 Share on other sites More sharing options...
redbrad0 Posted May 23, 2008 Author Share Posted May 23, 2008 function WriteToFile($file,$contents) { // IF the contents is empty lets just make it one space if ($contents=='' || empty($contents)) $contents = " "; // $file is the full path to the file $fh = fopen($file, "w") or die("Could not open file!"); fwrite($fh, $contents) or die("Could not write to file"); fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/#findComment-548259 Share on other sites More sharing options...
ballouta Posted May 23, 2008 Share Posted May 23, 2008 Many Thanks for you wish you good luck Quote Link to comment https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/#findComment-548288 Share on other sites More sharing options...
ballouta Posted May 24, 2008 Share Posted May 24, 2008 I have another question after reading the new email and copying the attachment to a specific directory (which is working).. How I can delete this used email? thanks Quote Link to comment https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/#findComment-548728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.