Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

getting attachment from the mail using mime type

Question posted by: realin (Familiar Sight) on May 12th, 2008 07:35 AM
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 !
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Atli's Avatar
Atli
Moderator
2,028 Posts
May 12th, 2008
10:40 AM
#2

Re: getting attachment from the mail using mime type
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.

Reply
realin's Avatar
realin
Familiar Sight
242 Posts
May 12th, 2008
01:13 PM
#3

Re: getting attachment from the mail using mime type
Quote:
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 !

Reply
Atli's Avatar
Atli
Moderator
2,028 Posts
May 13th, 2008
07:12 AM
#4

Re: getting attachment from the mail using mime type
You could simply use the file_put_contents function to store it in a temporary file and pass the name of that file to your second PHP file.

Something like:
Code: ( text )
  1. // Get the file data and mime type
  2. $fileData = "This would be the data from your email array";
  3. $fileMime = "image/jpeg"; // Or whatever
  4.  
  5. // Set file path and create the file name
  6. $filePath = "/path/to/your/tmp/dir/";
  7. $fileName = "temp_file_". microtime(true) .".tmp";
  8.  
  9. // Save file and print link, or error if it fails
  10. if(file_put_contents($filePath . $fileName, $fileData)) {
  11.   echo "Click <a href='readFile.php?fName={$fileName}&mime={$fileMime}'>here</a> to download</a>";
  12. else {
  13.   echo "Failed to save the file! Make sure you have write permission.";
  14. }

And then in the readFile.php:
Code: ( text )
  1. <?php
  2. // Get the values
  3. $filePath = "/path/to/your/tmp/dir/";
  4. $fileName = $_GET['fName'];
  5. $mime = $_GET['mime'];
  6.  
  7. // Make sure the file exists
  8. if(!file_exists($filePath . $fileName)) {
  9.   die("Invalid file name provided");
  10. }
  11.  
  12. // Set headers
  13. header("Content-Type: ". $mime);
  14. header("Content-Length: ".filesize($filename));
  15. header("Content-Disposition: attachment; filename=\"".basename($fileName)."\";" );
  16. header("Content-Transfer-Encoding: binary");
  17.  
  18. // Print file data
  19. readfile($filePath . $fileName);
  20.  
  21. // Delete file, if you want that
  22. unlink($filePath . $fileName);
  23.  
  24. exit();
  25. ?>

Thats at least the general idea.
Good luck :)

Reply
realin's Avatar
realin
Familiar Sight
242 Posts
May 13th, 2008
08:22 AM
#5

Re: getting attachment from the mail using mime type
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

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,098 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors