getting attachment from the mail using mime type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • realin
    Contributor
    • Feb 2007
    • 254

    getting attachment from the mail using mime type

    hiya all,

    I searched for a script and now i m able to receive the email from the server. Now the issue left is how to i download the attachments(if any) with that email. google led to me a wonderful link and i was able to download class called MimeDecode, which lets me parse headers into different array according to the content type.

    Well i get the attachment in the form of array, but the problem is now, how do i store/display it in browser. I know i have set the content-type in headers, but still it is not working can any one help me.

    http://pastebin.com/m61e9e752

    i have pasted the output of the array on above link..

    Cheers !!
    Realin !
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    If you have the mime/type and the data, you could try storing the data in a temp file and call it from another PHP script that uses the correct headers.

    Comment

    • realin
      Contributor
      • Feb 2007
      • 254

      #3
      Originally posted by Atli
      Hi.

      If you have the mime/type and the data, you could try storing the data in a temp file and call it from another PHP script that uses the correct headers.
      this is what i am planning how do i do it,
      i m planning to make some cases for different headers like text/plain,image/jpg etc
      then how do i store these ??
      i mean storing an image can be done using imagefromjpg() function, but is that the right way to do it ??

      Thanks for the reply :)
      Realin !

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        You could simply use the file_put_conten ts function to store it in a temporary file and pass the name of that file to your second PHP file.

        Something like:
        [code=php]
        // Get the file data and mime type
        $fileData = "This would be the data from your email array";
        $fileMime = "image/jpeg"; // Or whatever

        // Set file path and create the file name
        $filePath = "/path/to/your/tmp/dir/";
        $fileName = "temp_file_ ". microtime(true) .".tmp";

        // Save file and print link, or error if it fails
        if(file_put_con tents($filePath . $fileName, $fileData)) {
        echo "Click <a href='readFile. php?fName={$fil eName}&mime={$f ileMime}'>here</a> to download</a>";
        else {
        echo "Failed to save the file! Make sure you have write permission.";
        }
        [/code]
        And then in the readFile.php:
        [code=php]
        <?php
        // Get the values
        $filePath = "/path/to/your/tmp/dir/";
        $fileName = $_GET['fName'];
        $mime = $_GET['mime'];

        // Make sure the file exists
        if(!file_exists ($filePath . $fileName)) {
        die("Invalid file name provided");
        }

        // Set headers
        header("Content-Type: ". $mime);
        header("Content-Length: ".filesize($fil ename));
        header("Content-Disposition: attachment; filename=\"".ba sename($fileNam e)."\";" );
        header("Content-Transfer-Encoding: binary");

        // Print file data
        readfile($fileP ath . $fileName);

        // Delete file, if you want that
        unlink($filePat h . $fileName);

        exit();
        ?>
        [/code]
        Thats at least the general idea.
        Good luck :)

        Comment

        • realin
          Contributor
          • Feb 2007
          • 254

          #5
          hiya atil,

          gonna try this now..
          Well wat should i say.. ummm..
          THANKSSSSSSSS :p
          Heheheh..

          Will surely paste my working code, if it will work

          Comment

          Working...