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
  • 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!

Tags: extract images from emails and add them to ZenphotoCMS, add images to ZenPhoto via email, add photos to ZenPhoto via email, ZenPhoto, ZenPhoto Email extract, ZenPhoto Extension, ZenPhoto poll email, ZenphotoCMS

Continue Reading

Previous How to Install Windows Drivers from a Folder Using PowerShell
Next How to list all files with no Owner set

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

December 2023
MTWTFSS
 123
45678910
11121314151617
18192021222324
25262728293031
« Jul    

Archives

You may have missed

Identifying MFA-Enabled Domains
  • EMail
  • Office 365
  • Vulnerabilities

Email Security Analysis: A PowerShell Approach to Identifying MFA-Enabled Domains

The ICT Guy 0
convert unleashed to cloud
  • Connect the Classroom
  • Ruckus
  • Wireless

Convert ruckus unleashed to cloud in simple easy to follow steps

The ICT Guy 0
Sophos System Protection Service
  • Sophos
  • Veeam Backup and Replication

FIXED: Sophos System Protection Service (SSPService.exe) has high RAM Usage when using Splashtop

The ICT Guy 0
Office 365 Distribution Group Management
  • EMail
  • Office 365
  • Outlook

Automating Office 365 Distribution Group Management with PowerShell

The ICT Guy 0
NSW ConfigEdit Wizard
  • AI
  • Filtering
  • Made using OpenAI Chat GPT
  • Netsweeper

Simplifying Netsweeper Configuration with the NSW ConfigEdit Wizard PHP Script

The ICT Guy 0

Recent Comments

  • 4 pics one word on FIXED: Removing KB5000802 and KB5000808 updates for Windows 10
  • Mark on FIXED: Removing KB5000802 and KB5000808 updates for Windows 10
  • softbigs on FIXED: Removing KB5000802 and KB5000808 updates for Windows 10
  • Muneeb on Reset HP Printer page count and or serial number
  • bizzehdee on Reverse engineering the Costa App bean QR codes
Copyright © All rights reserved. | CoverNews by AF themes.