Send files to a PHP app via email?
July 9, 2010 12:55 PM   Subscribe

I'm not a PHP expert, but I'm working on a simple photo gallery application, and I'd like to add the ability to send it files via email. I know of other products that have this feature, but in my Googling I haven't been able to find any information on how to get started. Might you have any info?
posted by crickets to Computers & Internet (12 answers total) 1 user marked this as a favorite
 
The simplest option would be to have some code running periodically (using 'cron' on a linux system, your control-panel if you have one should have such an option) that connects to a mailbox and downloads messages. PHP has an extension for imap that you might have installed, but you'll still be left with quite a bit of leg-work to parse things. The Zend Framework's Mail component is really simple to use, and well documented.

If you have your own mailserver (postfix, say) then you can setup mail aliases that run code instead of (or as well as) going into a mailbox. That's more efficient than polling a box, but the barrier to doing it is a little higher.
posted by gregjones at 1:06 PM on July 9, 2010


At the most basic level, sending e-mails via PHP is done via the mail() function.

(This is assuming that the SMTP settings in php.ini have been set on your webserver)
posted by schmod at 1:06 PM on July 9, 2010


Hold on. Read that backwards. You want to *RECEIVE* messages. My bad.
posted by schmod at 1:06 PM on July 9, 2010


Be warned, this is a path fraught with peril.

Drupal has a Mail Comment module that does something similar to what you want (poll an email server for structured messages indicating a reply to a thread). If you're unfamiliar with Drupal, it may be hard to parse, but the gist is there.

The trick for you is going to be in:

- Validating sources via email address
- Parsing out attachments
- Filtering out non-image attachments

Mind you, You want some security around this, as "FROM:" is an easy header to fake out, and an email gateway to accept binary data is a security hole waiting to be exploited.
posted by mkultra at 1:13 PM on July 9, 2010


is this a general site with php or something like word press?
posted by majortom1981 at 1:18 PM on July 9, 2010


You may find the info you need on this page. Though it's 8 years old it's still probably relevant.

As for the security concerns mentioned by mkultra, I recommend using a secret password as the body of every message, and accepting only inbound emails containing that string in the body. This provides an extra layer of protection beyond the easily-spoofable From: header.
posted by The Winsome Parker Lewis at 1:37 PM on July 9, 2010


The first thing you will have to do is forward email received from whatever_email_address through a script on your server.

Add this to the start of your PHP script
#!/usr/bin/php -q

Then you are going to have to parse the mail message through standard input:
- - - - - - - - - -
$email = "";
$fh = fopen( "php://stdin", "r" );

while( !feof( $fh ) )
$email .= fread( $fh, 8192 );

fclose( $fh );
- - - - - - - - - -
The next step will involve split the email up into parts (from, subject, headers, message). The headers end at the first occurrence of a blank line. Use the data found in the headers to reconstruct your image based on Content-Type:
posted by axismundi at 2:10 PM on July 9, 2010


You don't need to bother with handling the mail in the php code to do this. You can use something like procmail to filter incoming messages, and dump acceptable images in some specific folder. You can then have the php code go through the folder and pick up images for inclusion in the gallery.

Like everybody else says, this is one more input stream for your application, hence one more thing to worry about from a security point of view: You need to carefully sanitize the input e-mails and take some steps to prevent your app from becoming a goatse-by-e-mail server.
posted by Dr Dracator at 2:22 PM on July 9, 2010




For Drupal, use Mail Handler and Mailsave -- take a look at this tutorial.

BUT BUT BUT to be honest, the best way to do this is to use Posterous. You email Posterous an email with attachments, and Posterous auto-parses images and movies, and reposts it to a blog. This Autopost function currently supports Drupal, Wordpress, Movabletype, and other CMSes. I've used it before without a hitch.
posted by suedehead at 9:12 PM on July 9, 2010


Whoops, wrong url: it should be Posterous. Here is more details about the Autopost function.
posted by suedehead at 9:13 PM on July 9, 2010


I have written exactly such a system in the past. It was split into a couple of parts:

1. The web app that viewed the pictures: pretty simple script, read the files in a folder, display them as a list, so on.

2. A script to receive emails with pictures in: this needs to run periodically on a server, say every 15 minutes and will connect to a mailbox somewhere (suggest a free gmail account), pull in any emails that are there, check the sender and recipient are 'valid' (you don't want randoms filling your album with guff) then pull any images from the email. I went a little further and deleted them from the POP3 box and also used the subject line as the caption for the pictures. I was then left with a temporary folder with images and a small data file with image captions.

3. Post processing: this can be done as part of the script or as a separate script. It takes the images from step 2 and inserted them into into the album software.

I eventually created MicroAlbum to handle the image part as all I needed to do was drop the image into the album folder, MicroAlbum would handle everything else. The script was a Perl script that ran every 15 minutes on one of my servers, I found it easier that way.

If you want to get funky and you do run a mail server then you can setup a local alias to execute the script whenever an email comes in to the right address but that requires a bit more server knowledge and access to the mail server configuration.
posted by gaby at 11:15 PM on July 9, 2010


« Older Traffic Court Blues   |   Stuttering Bill Sent Me Newer »
This thread is closed to new comments.