Link to home
Start Free TrialLog in
Avatar of RichieP
RichieP

asked on

Saving email attachments to server via PHP script

So far I have been able to write a script that reads a pop3 mailbox and retrieves all the emails in there. From these it then takes the various information and puts it into a mySQL database. Thanks all fine.

However, where I'm having a problem is attachments. I'd like to be able to take these out of the email and save them onto my server is a specified directory. The attachments are usually graphics.

So basically, someone would email this email address and then a php script would read the pop3 account, take all the information put it in a database and save any attachments to a certain directory.

The last bit (attachments) is the only thing I can't seem to figure out myself and I'd be more than grateful if anyone could help me out.
Avatar of Hatemben
Hatemben

i suggest you this php class http://sourceforge.net/projects/phpmimeclass

someone have asked this before and i have done some code here : https://www.experts-exchange.com/questions/20502540/preg-replace-string-syntax-question.html
Avatar of RichieP

ASKER

This is fine for getting the information but how about saving the actual files onto server?
You will also need to parse the email replacing the img src's for what ever you are going to use. If this is going to be for seeing on the web, then I would recommend that you use a script to retrieve the image data.

i.e.

Instead of ...

<img src="something.gif">

you would ...

<img src="getimage.php?id=emailid&count=4">

Make sure the emailid is scrambled enough so that you don't have the capabality of ...

<img src="getimage.php?id=1&count=1">
<img src="getimage.php?id=1&count=2">
<img src="getimage.php?id=1&count=3">
<img src="getimage.php?id=1&count=4">
<img src="getimage.php?id=2&count=1">
<img src="getimage.php?id=2&count=2">
<img src="getimage.php?id=2&count=3">
<img src="getimage.php?id=2&count=4">
<img src="getimage.php?id=3&count=1">
<img src="getimage.php?id=3&count=2">
<img src="getimage.php?id=3&count=3">
<img src="getimage.php?id=3&count=4">

Richard.
If you are using the mimeclass above, then ...

$mime_block[] is an array of the mime parts.

So, assuming you have your email as ...

$cHTMLEmail;

then

$cHTMLEmail->mime_block[0]

is the actual data for the first attachment.

You can use ...

$cHTMLEmail->mime_block[0]->getMimeContentType()

to get the type if content and ...

$fp = fopen("image1.gif","wb");
fwrite($fp,$cHTMLEmail->mime_block[0]);
$fclose($fp);

should do the trick.

Obviously, you need to look into more about the types and creating the right sort of file (png/gif/jpg/mp3/txt/etc).

Watching out too for text and html email where both formats are provided by the sender.

Richard.
Avatar of RichieP

ASKER

Sounds pretty logical from what I can understand. I don't know much about the mimeclass or classes in general actually. Most of my programming has just been generally by hand and quite simple.

Where could I find out how to use classes? real basic steps basically on what you've told me above?

If you could help me out with the basics yourself though I'd be MORE than appreciative and pay you back in anyway I can.
Avatar of RichieP

ASKER

I'd prefer to say away from the mimeclass and write my own thing actually but if its easier and better using the mimeclass then I'll certainly give it a go.
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RichieP

ASKER

Do you know of any websites that have any tutorials on using Classes because I've downloaded this mimeclass and have NO idea where to start.
A class is pretty much self enclosed object.

You create an instance of a class and this allows you to access the classes methods/variables.

This is the example in the class.

<?PHP

// This is just an example how you could use the mime_email_class
// This includes the MIME class of course and an pop3 abstraction
// class, i dont know if the pop3 section is correct, but thats not
// the point in this example

include("../include/class.pop3.inc");
include("../include/mime_email.class.php");

// create new pop3 object
$myPOP3 = new pop3();

// login to pop3server
$myPOP3->login("username", "pw");

// get an email (msg_array) with some ID, dont ask me where you get it :))
// this email is in an array (each line in the email has an array index)
$msg_array = $myPOP3->get($id);

// create a new MIME EMAIL object
$md = new mime_email();

// submit the raw email (the message array)
$md->set_emaildata($msg_array);

// decode the message and you will get back an object of Type mime_email_class
$myEmail = $md->go_decode();

// if you want to dump the data in order to see if the attributes were filled corectly
// use $md->dump() or $md->dump(true) if you want to see also the raw email in front of
// it all
$md->dump(true);


// now you must work with the object, later on i will submit also an object description
// but for now you must just read out the attributes at mime_email.class.php
// this file has two classes, look out for them!
 
# close the POP session
$myPOP3->quit();

?>

This example does include getting the email from the email server. Assuming you are already doing that, this is a cut down example ...

<?php

include("../include/mime_email.class.php");

// create a new MIME EMAIL object
$md = new mime_email();

// submit the raw email (the message array)
$md->set_emaildata($msg_array);

// decode the message and you will get back an object of Type mime_email_class
$myEmail = $md->go_decode();

// if you want to dump the data in order to see if the attributes were filled corectly
// use $md->dump() or $md->dump(true) if you want to see also the raw email in front of
// it all
$md->dump(true);

?>

Try these out first and see what happens.

Richard.