How can I save email attachments to the server in PHP?
Solution 1
I found a quick guide how to treat emails with PHP: here.
At the bottom of that page there's a attachment-body echo
:
if (substr($ContentType,0,4) == "text") {
echo imap_qprint($fileContent);
} else {
echo imap_base64($fileContent);
}
I guess this is what you might need...
(edit: in your case if it's image always you can skip the if
part. And of course, save the file instead of echoing it:)
Solution 2
MIME data is base-64 encoded, so I think you should be able to decode it using base64_decode
Solution 3
you can use the imap_base64
function and just output that to a file, or use imap_savebody
Solution 4
Zend framework contains Zend_Mail, which should make reading mail messages much easier, and Zend_Mime, which I believe can parse a multipart mime message into a sensible data structure.
Oli
Hi, I'm Oli and I'm a "full-stack" web-dev-op. Eurgh. I'm also allergic to jargon BS. I spend most of my professional time writing Django websites and webapps for SMEs. I write a lot of Python outside of Django sites too. I administer various Linux servers for various tasks. I contribute to the open source projects that I use when I can. I'm a full-time Linux user and that has lead to helping other people live the dream. I am an official Ubuntu Member and I earnt my ♦ on SE's own Ask Ubuntu in 2011's moderator election. That's probably where I spend most of my unpaid time. I also run thepcspy.com which has been my place to write for the last decade or so. If you need to contact me for extended help, you can do so via my website, just remember that I have bills so if I feel your request is above and beyond normal duty, I might ask for remuneration for one-on-one support. For more social contact, you can usually find me (or just my computer) lurking in the Ask Ubuntu General Chat Room and on Freenode in #ubuntu and #ubuntu-uk under the handle Oli or Oli``.
Updated on June 07, 2022Comments
-
Oli almost 2 years
I've been battling PHP's email reading functions for the better part of two days. I'm writing a script to read emails from a mailbox and save any attachments onto the server. If you've ever done something similar, you might understand my pain: PHP doesn't play well with email!
I've connected to the POP3 server and I can iterate the files. Here's a rough outline of the code:
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; } } } }
I've marked where I'm stuck. I can use
imap_fetchbody($mbox, $message->msgno, $key+1)
but that gets me a bunch of data like this:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8S EhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEU Hh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAAR CAHiAi0DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAA AgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkK FhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWG h4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl 5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREA ...
I'm lead to believe that this is MIME data. I need it as an image! I've seen several classes bombing around the internet that claim to do the required wizardry. I can't get any of them to work. I don't understand why I'm finding this so hard!
In short, I'm looking for something that can turn a raw MIME string into real data.
-
Oli over 15 yearsimap_savebody (from my recent experience) saves the encoded data (just as if you ran fetchbody)
-
Oli over 15 yearsI'd love to be able to deploy on the Zend Framework but hosting limitations restrict it. It's a real pity because the mail-reading functionality in plain PHP is twisted beyond all logical comprehension.
-
Oli over 15 yearsWorked like a charm. Yeah I'm already doing the content-type-detection (with my switch-statement) so I only needed the imap_base64.
-
TangQisen over 15 yearsIs it the size of the framework that's holding you back? Our web apps bundle zend framework for when it's not available on the customer's web server.