php – imap – 获取附件

前端之家收集整理的这篇文章主要介绍了php – imap – 获取附件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从此电子邮件获取附件?

这个电子邮件是从一个苹果电脑发送的,而且电子邮件的结构并不像任何其他的(惊喜).这里配置的部分比其他的更深一些.

该脚本与每个其他电子邮件一起使用,其中文件的部分位于第一个维度,但不与此一个

$part-> dparameters [0] – >值返回文件名,但strlen($data)返回0

imap流

  1. $structure = imap_fetchstructure($this->stream,$this->msgno);
  2.  
  3. if(isset($structure->parts)){
  4. print_r($structure->parts);
  5. $this->parse_parts($structure->parts);
  6. }
  7.  
  8. function parse_parts($parts){
  9. foreach($parts as $section => $part){
  10. if(isset($part->parts)){
  11.  
  12. // some mails have one extra dimension
  13. $this->parse_parts($part->parts);
  14.  
  15. }
  16. elseif(isset($part->disposition)){
  17. if(in_array(strtolower($part->disposition),array('attachment','inline'))){
  18. $data = imap_fetchbody($this->stream,$this->msgno,$section+1);
  19. echo $part->dparameters[0]->value.' '.strlen($data)."\n";
  20. }
  21. }
  22. }
  23. }

的print_r

  1. Array
  2. (
  3. [0] => stdClass Object
  4. (
  5. [type] => 0
  6. [encoding] => 0
  7. [ifsubtype] => 1
  8. [subtype] => PLAIN
  9. [ifdescription] => 0
  10. [ifid] => 0
  11. [lines] => 15
  12. [bytes] => 173
  13. [ifdisposition] => 0
  14. [ifdparameters] => 0
  15. [ifparameters] => 1
  16. [parameters] => Array
  17. (
  18. [0] => stdClass Object
  19. (
  20. [attribute] => CHARSET
  21. [value] => us-ascii
  22. )
  23.  
  24. )
  25.  
  26. )
  27.  
  28. [1] => stdClass Object
  29. (
  30. [type] => 1
  31. [encoding] => 0
  32. [ifsubtype] => 1
  33. [subtype] => MIXED
  34. [ifdescription] => 0
  35. [ifid] => 0
  36. [bytes] => 23420
  37. [ifdisposition] => 0
  38. [ifdparameters] => 0
  39. [ifparameters] => 1
  40. [parameters] => Array
  41. (
  42. [0] => stdClass Object
  43. (
  44. [attribute] => BOUNDARY
  45. [value] => Apple-Mail=_800896E0-A9C9-456E-B063-79CED9DD4FD7
  46. )
  47.  
  48. )
  49.  
  50. [parts] => Array
  51. (
  52. [0] => stdClass Object
  53. (
  54. [type] => 0
  55. [encoding] => 0
  56. [ifsubtype] => 1
  57. [subtype] => HTML
  58. [ifdescription] => 0
  59. [ifid] => 0
  60. [bytes] => 136
  61. [ifdisposition] => 0
  62. [ifdparameters] => 0
  63. [ifparameters] => 1
  64. [parameters] => Array
  65. (
  66. [0] => stdClass Object
  67. (
  68. [attribute] => CHARSET
  69. [value] => us-ascii
  70. )
  71.  
  72. )
  73.  
  74. )
  75.  
  76. [1] => stdClass Object
  77. (
  78. [type] => 3
  79. [encoding] => 3
  80. [ifsubtype] => 1
  81. [subtype] => PDF
  82. [ifdescription] => 0
  83. [ifid] => 0
  84. [bytes] => 17780
  85. [ifdisposition] => 1
  86. [disposition] => INLINE
  87. [ifdparameters] => 1
  88. [dparameters] => Array
  89. (
  90. [0] => stdClass Object
  91. (
  92. [attribute] => FILENAME
  93. [value] => 057 - LPJ - Stik og labels.pdf
  94. )
  95.  
  96. )
  97.  
  98. [ifparameters] => 1
  99. [parameters] => Array
  100. (
  101. [0] => stdClass Object
  102. (
  103. [attribute] => NAME
  104. [value] => 057 - LPJ - Stik og labels.pdf
  105. )
  106.  
  107. )
  108.  
  109. )
  110.  
  111. [2] => stdClass Object
  112. (
  113. [type] => 0
  114. [encoding] => 4
  115. [ifsubtype] => 1
  116. [subtype] => HTML
  117. [ifdescription] => 0
  118. [ifid] => 0
  119. [lines] => 75
  120. [bytes] => 4931
  121. [ifdisposition] => 0
  122. [ifdparameters] => 0
  123. [ifparameters] => 1
  124. [parameters] => Array
  125. (
  126. [0] => stdClass Object
  127. (
  128. [attribute] => CHARSET
  129. [value] => us-ascii
  130. )
  131.  
  132. )
  133.  
  134. )
  135.  
  136. )
  137.  
  138. )
  139.  
  140. )
您没有提供嵌套附件的正确部分号.您需要在递归步骤中传入节号.
  1. function parse_parts($parts,$parentsection = ""){
  2. foreach($parts as $subsection => $part){
  3. $section = $parentsection . ($subsection + 1);
  4. if(isset($part->parts)){
  5.  
  6. // some mails have one extra dimension
  7. $this->parse_parts($part->parts,$section . "." );
  8.  
  9. }
  10. elseif(isset($part->disposition)){
  11. if(in_array(strtolower($part->disposition),$section );
  12. echo 'Getting section ' . $section;
  13. echo $part->dparameters[0]->value.' '.strlen($data)."\n";
  14. }
  15. }
  16. }
  17. }

(未经测试,但应该做正确的事情…)

猜你在找的PHP相关文章