tags:

views:

7018

answers:

9

I'm writing a photo gallery webapp for a friend's wedding and they want a photo gallery for guests to submit the digital photos they take on the day.

After evaluating all the options, I've decided the easiest thing for users would be to let them use a familiar interface (their email) and just have them send in the pictures as attachments.

I've created an mailbox but now I need to connect and retrieve these attachments for automated processing for adding to the gallery system. But how? Are there any tutorials or prefab classes you've seen for doing this?

A: 

What mail server are you using? The process will depend on this information as each mail server will be storing emails in a different mailbox format.

workmad3
+1  A: 

I think you want a MIME message parser.

I've used this one before and it seems to work fine, although I haven't tested it on really big attachments (i.e. 2-3MB files you might get from digital cameras).

Have you already got a system for reading POP3 / IMAP mailboxes? There is another class on the same site which also works on POP3 (I believe there is also an IMAP one) - however if you will be downloading a fair volume maybe you'll want to investigate a few C-based solutions as I believe that one is pure PHP.

Phill Sacre
+2  A: 

What MTA are you using? If you use postfix + maildrop you can create a filtering rule that pipes certain messages through a PHP script that then handles the incoming mails. (google for maildrop and xfilter).

Armin Ronacher
Yeah postfix-routing looks like a good solution from where I'm sitting. Thanks!
Oli
+6  A: 

I used to do a lot of this before, but I can't find the code, here's a scaled down version I found. It should put you on the correct path. I used to run this type of script from a cronjob. Sorry I can't find the final version. ;(

// Open pop mailbox
if (!$mbox = imap_open ("{localhost:110/pop3/notls}INBOX", "user", "tester")) {
  die ('Cannot connect/check pop mail! Exiting');
}

if ($hdr = imap_check($mbox)) {
  $msgCount = $hdr->Nmsgs;
} else {
  echo "Failed to get mail";
  exit;
}

$MN=$msgCount;
$overview=imap_fetch_overview($mbox,"1:$MN",0);

for ($X = 1; $X <= $MN; $X++) {

  $file = imap_fetchbody($mbox, $X, 1);

  imap_delete($mbox, $X);
}

imap_expunge($mbox);
imap_close($mbox);

Good luck!

DreamWerx
If they control the receiving MTA I believe it's more elegant to have the MTA deliver to the program, instead of to an IMAP server, because it's simply fewer points of failure. That's not always an option, I concede.
Daniel Papasian
+4  A: 

If you're creating a dedicated mailbox for this purpose, using a filtering mechanism is almost definitely not what you want. Instead, you want to have the mailbox be a pipe to the application, and have the application simply read in the message from stdin, parse out the body, and MIME parse the body to get the attachments.

Having a mailbox be a pipe is supported by all the popular unix-based MTAs that I know of, such as sendmail, postfix, and qmail. Generally you define it in your aliases file, like so:


# sendmail or postfix syntax
msgsubmit: "| /usr/bin/php ~path/to/example.php"

Then mails to msgsubmit@ get routed to a php program for delivery.

This has the advantage of not relying on an IMAP server or any other server beyond the MTA being alive, and it works fine as long as you have control over the MTA of the destination host. Filtering is what you'd want if you wanted all messages on a system to be inspected by the script, which I'm guessing is not the case.

If you want a copy kept in a mailbox somewhere (not a bad idea) simply define the alias to go to multiple addresses, like so:


msgsubmit: "| /usr/bin/php ~path/to/example.php", msgsubmit-box

Or postfix virtual format:


msgsubmit
    "| /usr/bin/php ~path/to/example.php"
    msgsubmit-box

Daniel Papasian
+3  A: 

Have you considered using Google's Picasa Web Albums? You can set up an email address to send photos to and share them online. You can then get an RSS feed of these photos, which most programmers are more familiar with than MTAs.

Liam
This is a neat alternative! Google advises against sharing your secret key, however, so you might want to shroud it by having your MTA alias a certain address to it, with something like this in your alias file:msgsubmit: [email protected]
Daniel Papasian
plus one to you for thinking outside the box
Nir Gavish
A: 
A: 

oh man..sorry guys.i can't display the full code.email me at [email protected] to get the fully code.sorry,mayb b'coz of the length of the code cause this problem occur.

A: 

[email protected] doest work. Please provide an alternative email address.

Thanks,

BP