Link to home
Create AccountLog in
Avatar of tkorth
tkorth

asked on

extract inline images/files from emails messages

Hi.   Let me start with my ultimate goal:  Forward an email with inline picture (in the body, not "attachments" as such) to my Wordpress blog and have it post.   I'm having a problem with getting the inline attachments to be saved.  I just get the text without the pictures.   Note: sending an email with a normally attached picture works just fine.

Looking at this post: https://www.experts-exchange.com/questions/21721792/seeking-optimal-way-to-parse-an-email-sent-to-my-PHP-script-via-PIPE.html

Seems to point me in the right direction.  But I'm particularly interested in the response by Mensuck about the php script.  

Mensuck, are you out there and can you post that script?  

Or does anyone know of something akin to Mensuck's script?

Thanks.
Avatar of waygood
waygood

You should be able to extract inline images the same way as attachments, as they are seperate from the body of the message.
If you send one to your script and echo out the complete contents, the seperators should be visible for you to adjust your existing code to include them.
<?php
$nl="<br/>\n";
 
$site=array();
$site['email_address']  = "content@somewhere.co.uk";
$site['email_name']     = "Content from Somewhere";
$site['pop_host']       = "pop.somewhere.co.uk";
$site['host']           = "auth.smtp.somewhere.co.uk";
$site['username']       = "content@somewhere.co.uk";
$site['password']       = "password";
$site['authentication'] = TRUE;
$site['folder']         = "../safe/";
 
$reader=new email_reader_class();
$reader->set_account($site['pop_host'], $site['username'], $site['password']);
$ok=$reader->connect();
if($ok)  // if connected
{
	$count=$reader->get_count();
if($report) echo $count.' emails on server<br/>';
	if($count>0)  // if emails to process
	{
		$other_emails=TRUE;
		while($other_emails)  // step through emails
		{
			$processed=FALSE;
			$head=$reader->get_header();
			$from_address = trim($head['from'][0]['mailbox']) . "@" . trim($head['from'][0]['host']);
if($report) echo 'From: '.$from_address.'<br/>';
			if(eregi("@somewhere.co.uk", $from_address))
			{
				$message_id="";
				if(isset($head['message_id'])) $message_id=$head['message_id'];
if($report) echo 'ID: '.$message_id.'<br/>';
				if(!empty($message_id))
				{
					$sql="SELECT * FROM email_processing WHERE message_id='".addslashes($message_id)."'";
					$result=run_mysql_query($sql);
					if(mysql_num_rows($result)==0)  // if email hasnt been processed already
					{
if($report) echo 'Not previously processed<br/>';
						$body=trim($reader->get_body());
						$attachments=$reader->get_attachments();
						while(list($key,$value)=each($attachments))
						{
							if(is_dir($site['folder']))
							{
								$filename=$site['folder'].$value['filename'];
								$filehandle=fopen($filename, "ab");
								fwrite($filehandle, $value['filedata']);
								fclose($filehandle);
							}
						}
					}
				}
				if($processed)
				{
					$other_emails=$reader->delete_current_email(); // <- when live
				}
				else
				{
					$other_emails=$reader->next_email(); // <- when testing
				}
			}
			else
			{
				$other_emails=$reader->next_email();
			}
		}
	}
	else
	{
		echo "No emails to process".$nl;
	}
	$reader->disconnect(); 
}
else
{
	echo $reader->error;
	echo $nl;
}
 
?>

Open in new window

class.email-reader.php.txt
Avatar of tkorth

ASKER

Thanks.  Let me get that hooked in and see if I can get it fixed.
Avatar of tkorth

ASKER

Thanks Waygood.  I used your code in the code box to check the email and gave it a require class.email-reader.php  statement.  It logs in and checks the emails just fine, but it only saves the image that has been attached normally.  It will not save the image that appears in the body of the email.

In looking through the class.email-reader.php in the get_attachments() function it calls
if (strtoupper($att_obj->disposition) == "ATTACHMENT").

However, in my inline email, there is no "Content-Disposition: attachment" statement before the encoded attachment.  Therefore the class.email-reader doesn't pick up the picture as an attachment. (I checked and it doesn't seem to do this on any of my inline email examples.)

I changed that line of code to    
if (strtoupper($att_obj->type) == "5")    
and that works better.  It now catches most of the inline pictures that I throw at it.   But not all.

Is there a better way of doing this?

I've attached two text files that are the body of two demo emails to demonstrate the difference - one with a normally attached picture and one with an 'inline' picture.

If you like, I can email the actual emails.
Thanks
jpeg-attached.txt
jpeg-inline.txt
ASKER CERTIFIED SOLUTION
Avatar of waygood
waygood

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of tkorth

ASKER

Notes for myself... copied from:
https://www.experts-exchange.com/questions/22871080/php-save-email-attachments-to-server.html

            $message["attachment"]["type"][0] = "text";
            $message["attachment"]["type"][1] = "multipart";
            $message["attachment"]["type"][2] = "message";
            $message["attachment"]["type"][3] = "application";
            $message["attachment"]["type"][4] = "audio";
            $message["attachment"]["type"][5] = "image";
            $message["attachment"]["type"][6] = "video";
            $message["attachment"]["type"][7] = "other";
Avatar of tkorth

ASKER

Thanks for the help.  I've got it saving both the inline attachments and also the regular attachments.  Now I need to work that into the blog-by-email scripting.  Fun fun!
Oooh! Lovely, just copied and pasted that into my code for reference. Thanks :-)