我正在尝试抓取电子邮件并显示电子邮件的正文以及下载任何附件的链接。我在其他没有附件的电子邮件中取得了成功,但有了这个,我甚至找不到身体。
我的问题:
提前致谢!
-
预期的电子邮件正文:
This s the second test with attachments
预期的电子邮件附件:
代码:
$mbox = imap_open($mailbox_path, $username, $password);
$msgs = imap_sort($mbox, SORTDATE, 1, SE_UID);
$msguid = array_shift($msgs);
$email['msgno'] = imap_msgno($mbox, $msguid);
$email['body'] = imap_fetchbody($mbox, $email['msgno'], 2);
$email['printable_body'] = quoted_printable_decode($email['body']);
$email['attachments'] = getAttachments($mbox, $email['msgno']);
print_r($email);
function getAttachments($connection, $message_number) {
$structure = imap_fetchstructure($connection, $message_number);
$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($connection, $message_number, $i+1);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
return $attachments;
}
输出:
Array
(
[msgno] => 1
[body] => bWFnZVJlYWR5ccllPAAAA09JREFUeNqUVG1MTmEYfr7PKzYjZvODZlr+hFlo813GD5+LTKtNs6aX
Rn4oP9iyUsNGXsQP6x0ipBGaz5mak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SYLjLhlKmz1BikM+IOlx9
/e9ThRgDn1jDUMd3+NOc/n4o4ifgRyH4e8FFEurUBtMHz155vYGg8xNMy9qM5THbsR8P9N/s+TmU
X7o9VklZgEY4UoijuNDtaMZ0NGClbdt3Qt1mIDtlvjUQYM/YgF0LwPYJ3FWwzaOUoA+0jVikgnD3
7r+Aec93AQYAtxlOu/e3wtgAAAAASUVORK5CYII
[attachments] => Array
(
[0] => Array
(
[is_attachment] =>
[filename] =>
[name] =>
[attachment] =>
)
[1] => Array
(
[is_attachment] => 1
[filename] => attachment.png
[name] => attachment.png
[attachment] => ‰PNG
IHDR‰
tEXtSoftwareAdobe ImageReadyqÉe<OIDATxÚ”TmLNa~¾Ï+6#fóƒfZþ„Yhó]ÆŸ‹L«M³¦—F~(?زRÃF^Äë"¤šÏ™š‘úÇ0ËÌ…™z;Ïq-ôg»Ï}žû¹ïë¹îçPòÏ´eé²Ìx«Û\aÛf,L”ùX¾r¥Œ×5„ý+ØÌUYÁ1B¿PŠJå; ð-åxy~¢?¹é~N\â1y£µN©\Ë(„l8¼kÚòfŸ@4ƒR:~ý2„ÓdH6d4ç|!HÄ0Î+=0o¿©æ$‘RuI)1Ù›$XÍs]÷9–íMÆw•KÜö^>>ü³±×&ÑØâu[¸£uœ2ò>ÊèJWkóZp`‰?ŸÀî¥÷Åqœ‘Xd».yt-XzÚ‹eRFŠs ÍBŠaŒ±Hõìt0ÔjÈ8ìÇÃÇ9»ÿ+¶O@tlœ§ ,uœ?¤îÊѽm+6n§°Ï‡L€HØS¡kã/~ÅþUÃU›8ê’Š´jÀ45áûJ˜££8åIÌeU^Ã`oÕšV]8RB"2DÐlœÚÉo„NĘÜ@ʯҶ*¬'A4ÊÐ ½öŠe%ßzÇÿ˜¹mwœR*²Çgø&B;úê@QHI5ÕPÆe¨ZØr¤’÷Î
ŸüI¨'å
E¤i™i8õ£ÔDç‘RSpg^gNñAÒÝm&£Ÿ°¯ %BnõUÑ«“%ãR† h¯‹ø.ïq¶$Ž÷j—À3æ>'Ä{å—%æ.N/µv.¢k£aÞŠú=`Œ¾Å¼e`¿Œ3…Cf8Ú)l]ÿ9"C)ÅP¡Ý¡p~jÚÖWœÓ–'0_阻°ºi0öΫŒvõUË¢‘nXpÏ7Ú»bÛ²R]hï®ÖÿRzê2û
•hGw€ií®Ü47`¸Ë†R¦ÏPbψ:\}ýïS…ŸXÃPÇwøÓœþ~(â'àG!ø{ÁEêÔÓÏ^y½ óLËÚŒå1Û±ôßìù9”_º=VIY€F8Rˆ£¸ÐíhÆt4`¥mÛwBÝf ;e¾5`ÏØ€]Àö ÜU°Í£” ´X¤‚p÷î¿€yÏw·N»÷·ÂØIEND®B`‚
)
)
)