两天的大部分时间里,我一直在与PHP的电子邮件阅读功能作斗争.我正在编写一个脚本来读取邮箱中的电子邮件并将所有附件保存到服务器上.如果你曾经做过类似的事情,你可能会理解我的痛苦:PHP在电子邮件方面表现不佳!
我已连接到POP3服务器,我可以迭代文件.这是代码的大致轮廓:
if (!$mbox = imap_open ("{myserver.com:110/pop3/notls}INBOX", "u", "p")) die ('Cannot connect/check mail! Exiting'); if ($hdr = imap_check($mbox)) $msgCount = $hdr->Nmsgs; else die ("Failed to get mail"); foreach ($overview as $message) { $msgStruct = imap_fetchstructure($mbox, $message->msgno); // if it has parts, there are attachments that need reading if ($msgStruct->parts) { foreach ($msgStruct->parts as $key => $part) { switch (strtoupper($part->subtype)) { case 'GIF': case 'JPEG':case 'PNG': //do something - but what?! break; } } } }
我已经标记了我被卡住的地方.我可以使用,imap_fetchbody($mbox, $message->msgno, $key+1)
但这会得到一堆像这样的数据:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8S EhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEU Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAAR CAHiAi0DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl 5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA ...
我认为这是MIME数据.我需要它作为一个图像!我已经看到几个班级在互联网上爆炸声称要做必要的魔法.我不能让他们中的任何一个工作.我不明白为什么我这么难找到!
简而言之,我正在寻找可以将原始MIME字符串转换为实际数据的东西.