How to extract images from EMails and add them to a ZenphotoCMS album

Goal
To extract images from emails and add them to a ZenphotoCMS album by simply emailing them to a dedicated mailbox.
Solution
1 – Create a dedicated email account, photos@yourdomain.com for example and record the details, you will need these later.
2 – Under albums on you web server create a folder called Inbox

3 – Edit the php script below to reflect your email account and web server settings
<?php function getFileExtension($fileName){ $parts=explode(".",$fileName); return $parts[count($parts)-1]; } $imap = imap_open("{yourdomain.com:143}INBOX", "photo@yourdomain.com", "PASSWORD") or die(print_r(imap_errors())); $message_count = imap_num_msg($imap); for ($m = 1; $m <= $message_count; ++$m){ $header = imap_header($imap, $m); print_r($header); $email[$m]['from'] = $header->from[0]->mailbox.'@'.$header->from[0]->host; $email[$m]['fromaddress'] = $header->from[0]->personal; $email[$m]['to'] = $header->to[0]->mailbox; $email[$m]['subject'] = $header->subject; $email[$m]['message_id'] = $header->message_id; $email[$m]['date'] = $header->udate; $from = $email[$m]['fromaddress']; $from_email = $email[$m]['from']; $to = $email[$m]['to']; $subject = $email[$m]['subject']; echo $from_email . '</br>'; echo $to . '</br>'; echo $subject . '</br>'; $subject = preg_replace('/\s+/', '', $subject); //subject minus spaces $structure = imap_fetchstructure($imap, $m); $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($imap, $m, $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']); } } } } $mypath = './albums/Inbox/'; $DateTime = date("Ymd_His", strtotime('+0 hours')); echo $attachment_name; foreach ($attachments as $attachment) { file_put_contents( $mypath . $subject . "_" . $DateTime . "_" . $attachment['name'], $attachment['attachment']); } } //emtpy the inbox and close the connection @imap_delete($imap,'1:*'); imap_expunge($imap); imap_close($imap); ?>
4 – Upload the php script to your website
5 – Create a cron job to manually poll the file every hour
The cron should consist of the below command and run either hourly or every 15 minutes.
php -q /home/username/public_html/yourdomain.co.uk/fetch_email.php >/dev/null 2>&1

You should now be able to email photos to the address you created, wait for the cron job to run, the files will then appear in the Inbox album ready to be sorted further.
Prerequisites
ZenphotoCMS – The simpler media website CMS will need to be preinstalled on your web hosting, you will also need FTP access and a limited working knowledge of PHP and how to setup an email account (however it is pretty straight forward)
External Links
ZenphotoCMS – The simpler media website CMS
What data in the script needs to be changed?
After several attempts the script will not work.
You should just need to change this line – $imap = imap_open(“{yourdomain.com:143}INBOX”, “photo@yourdomain.com”, “PASSWORD”) or die(print_r(imap_errors()));