Jump to content

Recommended Posts

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;

Link to comment
https://forums.phpfreaks.com/topic/82546-saving-wav-file-from-a-email-attachment/
Share on other sites

  • 5 months later...
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);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.