PHP script to download files from GMail

Wed Dec 15 1:20:07 2010 EST (-0500 GMT)

While my wife was getting a valuable two hours of sleep before she was woken with contractions signaling the birth of our first born child 19 hours later, I was not quite tired enough to sleep. UnknowingIy missing out on my last chance to sleep for a while, I was typing away at a PHP script to allow us to share pictures of our new baby – whenever he was going to arrive – with our friends and family privately with nothing more complex than the ability to eMail photos from my iPhone.

It was important to us to be able to share these pictures of our new child, but also to protect our child’s image from the very public exposure of the public internet and the still too public (or at least, un-trustable privacy of) Facebook.

Here’s how I was able to achieve this with a web server, some PHP, a GMail account and my iPhone:

Creating Galleries
First off I created a folder with standard Apache Basic Auth settings and let our friends and family know the simple username and password. I also turned on WebDAV access for the geekier viewers. In order to construct interesting galleries I placed a copy of Qdig, a Quick Digital Image Gallery PHP script to create galleries and thumbnails on the fly (and secured it against the WebDAV access). I had to increase the amount of RAM PHP could use, but otherwise it was very easy to implement.

Getting eMail Attachements from GMail with PHP

I created a new GMail account (specifically through my Google Apps domain) to receive all of these cute baby pictures via eMail from my iPhone. The next step was to check it automatically and store the attached pictures in a specific folder.

I wrote a PHP script, fetch_mail.php, that POP’s the GMail account, looks at new mail for attachements, and saves the attachements in the protected folder.

The full PHP file, save my account details, is here: mattclare.ca/~mclare/blog/fetch_mail.phps

The configuration settings:
$user=[email protected]//GMail address 
$pass=“your password”//GMail address
$save_dir ‘./’//Folder to save the files into
//lower case array of EMail address that are allowed to send.  Don’t set if you want to allow anyone
$senders = array([email protected],[email protected]);
$extensions = array(‘png’,‘jpeg’,‘jpg’,‘gif’); //lower case file extensions to allow.  Don’t set if you want allow any file
$prepend_date true;  //Add date and time to start of file names to prevent filename collisions
$status true//Should a status report be reported at the end?

The GMail imap_open string for POP:

// GMail with pop3
$authhost=“{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX”;

$connection imap_open($authhost$user$pass);
imap_errors();

Then over a hundred more lines of PHP to look for an attachement and to then save it to the designated folder.

Running the PHP Script
I added the following to my server’s crontab to have it check for new mail every ten minutes.

*/10 * * * * nice php -f ~/bin/baby_pictures/fetch_mail.php

I ran the script from the command line, but it would work through the web.

That’s It

Hopefully if you know a little PHP and have a web server at your disposal this script will help you move files from GMail to your web site. I know we’ve really appreciated sharing our joy this way.