941,524 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1984
  • PHP RSS
Mar 8th, 2010
0

Email Attachment Handling - (Media files)

Expand Post »
Ok, so basically I want to be able to do this. I have it all down from obtaining the content from the e-mail and saving the attachment, but when I save the file's content into a file (permissions aren't a problem, as you'll see in the code) it doesn't show it at all. It just says the image's path when I try to pull it up in my browser, so I'm guessing there's got to be a mime-type or decoding error happening.

What I'm doing is reading a catch-all mailbox account, and pulling any attachments (only images and videos, though, but that's not the problem) that are within the e-mail(s). Later on, I'll delete the e-mail message, but that's not important right now. I'm just having some attachment parsing problems due to either encoding problems or mime-types. Hopefully it's an easy fix.

Here's the code I'm using:

PHP Syntax (Toggle Plain Text)
  1. if ($imap = imap_open("{".$server."/imap/notls}INBOX",$user, $pass)) {
  2. /*echo "Total Messages: ". sizeof(imap_headers($imap));
  3. $headers = imap_headers($imap);
  4. echo "<pre>\n";
  5. print_r($headers);
  6. echo "</pre>\n";
  7. echo "<hr />\n";
  8. echo "<pre>\n";*/
  9. $y = 0;
  10. $attachments = array();
  11. foreach (imap_headers($imap) as $head) {
  12. print_r(imap_fetchstructure($imap, $y));
  13. $y++;
  14. }
  15. foreach (imap_headers($imap) as $head) {
  16. //electrictoolbox.com reference
  17. $structure = imap_fetchstructure($imap, $y);
  18. if(isset($structure->parts) && count($structure->parts)) {
  19. for($i = 0; $i < count($structure->parts); $i++) {
  20. $attachments[$i] = array(
  21. 'is_attachment' => false,
  22. 'filename' => '',
  23. 'name' => '',
  24. 'attachment' => ''
  25. );
  26.  
  27. if($structure->parts[$i]->ifdparameters) {
  28. foreach($structure->parts[$i]->dparameters as $object) {
  29. if(strtolower($object->attribute) == 'filename') {
  30. $attachments[$i]['is_attachment'] = true;
  31. $attachments[$i]['filename'] = $object->value;
  32. }
  33. }
  34. }
  35.  
  36. if($structure->parts[$i]->ifparameters) {
  37. foreach($structure->parts[$i]->parameters as $object) {
  38. if(strtolower($object->attribute) == 'name') {
  39. $attachments[$i]['is_attachment'] = true;
  40. $attachments[$i]['name'] = $object->value;
  41. }
  42. }
  43. }
  44.  
  45. if($attachments[$i]['is_attachment']) {
  46. $attachments[$i]['attachment'] = imap_fetchbody($imap, $y, $i+1);
  47. if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
  48. $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
  49. } elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
  50. $attachments[$i]['attachment'] = imap_qprint($attachments[$i]['attachment']);
  51. }
  52. }
  53. }
  54. }
  55. $y++;
  56. }
  57. foreach ($attachments as $file) {
  58. $filename = $file['name'];
  59. $extension = end(explode(".", $file['name']));
  60.  
  61. if (in_array($extension, $extensions)) {
  62. if ($extension == "mpeg" || $extension == "mpg" || $extension == "avi" || $extension == "wmv") {
  63. //it's a video
  64. } else {
  65. //it's an image
  66. $content = $file['attachment'];
  67. $explode = explode(".".$extension, $file['name']);
  68.  
  69.  
  70.  
  71. }
  72.  
  73. }
  74. }
  75. } else {
  76. //handle error
  77. exit;
  78. }
Last edited by sutt0n; Mar 8th, 2010 at 4:02 pm. Reason: Specified my problem in more detail.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sutt0n is offline Offline
8 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: simple php array key question i cannot figure out plz help!
Next Thread in PHP Forum Timeline: IRC bot keeps disconnecting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC