Skip to content

The ICT Guy

EduTech server engineer blogging about everything IT related

Primary Menu

The ICT Guy

  • Contact Us
    • Cookies & Privacy
  • Tools
    • Backup Register
    • Login System
    • MDT Silent Installs
    • OST Reports
  • Useful links
  • Home
  • Scripts
  • How to extract images from EMails and add them to a ZenphotoCMS album
  • Scripts

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

The ICT Guy 2
extract images from emails and add them to ZenphotoCMS
Contents show
1 Goal
2 Solution
3 Prerequisites
4 External Links
4.1 Found priceless insights in this blog? Support the author’s creativity – buy them a coffee!

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

email upload

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

2021 02 19 15 24 30 cPanel Cron Jobs and 3 more pages Work Microsoft​ Edge

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

Found priceless insights in this blog? Support the author’s creativity – buy them a coffee!

Post navigation

Previous: How to add a custom background to MDT
Next: How to effectively use Delivery Optimization to distribute Windows Update content to clients inside your LAN

More

PowerShell to create SharePoint Sites
  • Azure AD Connect
  • Azure AD Connect cloud provisioning
  • Microsoft
  • Powershell
  • Scripts
  • SharePoint

Using PowerShell to create SharePoint Sites

The ICT Guy 0
list all files with no Owner set
  • Powershell
  • Scripts
  • Server Migration

How to list all files with no Owner set

The ICT Guy 0
  • Powershell
  • Scripts
  • Windows 10

How to Install Windows Drivers from a Folder Using PowerShell

The ICT Guy 0

2 thoughts on “How to extract images from EMails and add them to a ZenphotoCMS album”

  1. Spa says:
    22 January 2023 at 1:20 PM

    What data in the script needs to be changed?

    After several attempts the script will not work.

    Reply
    1. The ICT Guy says:
      30 January 2023 at 3:32 PM

      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()));

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

If you found this blog helpful, would you consider buying me a Coffee?

Search

Other bits and bobs

  • Contact Us
    • Cookies & Privacy
  • Tools
    • Backup Register
    • Login System
    • MDT Silent Installs
    • OST Reports
  • Useful links

Categories

Tags

0x80070005 Active Directory apple ios Automation Azure AD BitLocker BitLocker Drive Encryption Coding Cybersecurity Domain Controller Domain Migration Efficiency Group Policy Hyper-V Hyper-V best practices IT Efficiency IT Management Made using OpenAI Chat GPT MDT MFA Microsoft Microsoft Teams Netsweeper Network Security Office 365 OneDrive Powershell PowerShell Scripting RDWeb Remote Desktop Remote Desktop Web Access Remote Working Scripting Server 2019 Server Guide Server Migration SharePoint SQL System Administration Teams User Experience Veeam Windows 10 Windows defender Windows Server

Posts by Date

October 2025
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Sep    

Archives

You may have missed

powershell-dhcp-analysis-script
  • DHCP
  • Featured Posts

Optimize DHCP Management with PowerShell: Comprehensive Analysis and Reservation Export

The ICT Guy 0
Free GPS Access Point Mapper
  • Wireless

Map Your World: Introducing Our Free GPS Access Point Mapper

The ICT Guy 0
2024-09-06 15_31_57-Complex Password Generator and 2 more pages - Personal - Microsoft​ Edge
  • Editors Picks
  • Featured Posts
  • Uncategorized

Elevate Your Online Security with Our Advanced Complex Password Generator

The ICT Guy 0
2024-09-06 15_18_08-Strong Password Generator and 2 more pages - Personal - Microsoft​ Edge
  • World Password Day

Boost Your Online Security with Our Advanced Password Generator

The ICT Guy 0
Wi-Fi QR Code Generator
  • Editors Picks
  • Featured Posts
  • Wireless

Wi-Fi QR Code Generator: Simplifying Network Sharing

The ICT Guy 0

Recent Comments

  • Pepper on DNS resolution based on IP subnet using Get-DnsServerQueryResolutionPolicy
  • CharlotteAster on Find long folder and file paths with PowerShell
  • Vetri on Webcam not working on Lenovo laptop
  • Mark on Reverse engineering the Costa App bean QR codes
  • Kyle Schuler on Active Directory Visio Mapper
Copyright © All rights reserved. | CoverNews by AF themes.