Making Gallery2 accept photos via email.

Gallery2 doesn’t support emailing photos to it by default, so I had to make alot of changes to get it working. Look behind the cut if you’re interested in how I did it.

First order of business was writing a perl script that would parse an email message, extract the images, and then use a script called galleryadd.pl to upload them to Gallery2. I’m using a perl module called MIME::Parser to perform most of the tricky stuff. I haven’t taken any time to really clean up the code yet. This is what I’ve thrown together tonight:

#!/usr/bin/perl -w

use strict;
use MIME::Parser;

# Created this as a hash so that it can handle multiple people
# each are assigned a unique email address, and their photos
# are put into the proper album.
my $config = {
   'address@domain.com' => {
      user => 'user',
      pwd => 'password',
      album => 'XXX'
   }
};

print STDERR "Starting handleMoBlog.pl $$ - " . time . "\\n";

my $tmpdir = '/home/username/tmp';
my $parser = new MIME::Parser;
$parser->output_dir( $tmpdir );

my $email = $parser->parse( \*STDIN );

my $subject = $email->head->get( 'subject', 0 );
$subject = trim( $subject );

my $to = $email->head->get( 'to', 0 );
$to = lc( trim( $to ));

print STDERR "to: " . $to . "\\n";
print STDERR "subject: " . $subject . "\\n";

foreach my $part ( $email->parts ) {
   # loop through each attachment.  we only care about the images.
   if ( $part->mime_type =~ /^image\/(.*?)$/ ) {
      my $file = time . "." . $1;

      my $handle = $part->bodyhandle->open('r');

      # slurp mode.  changes the record delimiter from carriage return 
      # to nothing so the whole file gets read in.
      local($/) = undef;

      my $bits = $handle->getline;
      $handle->close();

      open IMG, ">$tmpdir/$file" or die $!;
      print IMG $bits;
      close IMG;

      `/home/username/bin/galleryadd.pl -q -g http://www.username.com/gallery -G 2 -u $config->{$to}->{user} -p $config->{$to}->{pwd} -a $config->{$to}->{album} -C "$subject" $tmpdir/$file`;

      print STDERR "mime_type: " . $part->mime_type . "\\n";
      print STDERR "attached: $file\n";
   }
}

$email->purge;

print STDERR "Ending handleMoBlog.pl $$ - " . time . "\\n";

sub trim {
   my $str = shift;

   $str =~ s/\\n//g;
   $str =~ s/\\r//g;

   return $str;
}

Second of all, I needed to set everything up so that procmail was calling my script that would process the emails sent by my cell phone. That required me creating a file called .forward.postfix in my home directory up on the server. The file only needed this line in it:

"|/usr/bin/procmail -t"

Finally, need a procmail script that calls my handleMoBlog.pl script:

DEFAULT=$HOME/Maildir/
MAILDIR=$HOME/Maildir
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/log
SHELL=/bin/sh

:0 
* ^TOaddress@domain\.com
| /home/username/bin/handleMoBlog.pl >> /home/username/tmp/handleMoBlog.log 2>&1

It’s working, you can see it in action on my site. Like I mention in one of the comments in the handleMoBlog.pl script, the config hash is there so that it can handle multiple users. I set it up in a really flexible way. All you have to do is create a seperate email address for each seperate user. You can set them up with their own albums, or not. You can set them up with their own Gallery2 user, or not. All you have to do is make certain that each of them has an entry in the config hash, and in the procmail script.

Leave a Reply

You must be logged in to post a comment.