Need help with BASH(?) script.... ( Decode mail attachements and save file)

software development

#1

I’ve set up a couple of zenphoto installations for my wife. No problems there, even tweaked a thing or two for our liking. :slight_smile:

In the process I found out I really like a couple of things about zenphoto and want to use it to replace my Photobucket account. Now the way I use my Photobucket account is what’s important. The only way I use it is to email photo’s to my Photobucket account from my phone (you setup an inbound email address @photobucket.com in your photobucket settings), and have them show up online shortly thereafter.

I’m posting this here rather than the zenphoto forum because the answer really deals more with a script that will probably be more localized to dreamhost than the universal answer for zenphoto.

For those that are not familiar with zenphoto, all that needs to happen to get a photo imported into zenphoto is for the jpg gif etc to get moved into /{DOMAIN}/zenphoto/albums/ (or any sub-directory underneath it). We send photo’s there directly via ftp… works great…. lasts a long time….

(PS – If you want to try this at home you will have to download and install your own zenphoto, the ’one click’ install doesn’t seem to give you access to the /albums/ directory… or it least it didn’t a year ago when I last messed with the ‘one click’ install.)

I poked around today in the dreamhost panel and found how to set up an email filter to send mail from a real internet email address on the mail server to a shell user. I set up a test and can find my email in /home/{USER}/Maildir/new/ but of course the issue is extracting the file(s) from the encoded email and moving it to the proper directory….

What sounds logical to me in this case is a bash script (written in pseudocode below because the syntax etc is what I need help with) called by cron every 5 minutes that:

((sorry in advance… i can’t figure out how to indent the code below in this forum–space / tab neither one stays in place…))

((assume the mail filter set in the panel only forwards mail FROM a specific address (I.E. my phone)… not perfect security but it works for me))

basepath = “/home/{USER}/{DOMAIN}/zenphoto/albums/”
count = “0”
While mailfile exists in /home/{USER}/Maildir/new/

do
Set picfolder BASEPATH “/mobile/” ;the default location
If Subject = “sub_path_that_exists” then set picfolder = BASEPATH “subpath_that_exists” ; or it stays in the default
While another_attachment_exists in mailfile

do
Decode attachment and move to picfolder
Count = count 1
done

If count = 0 forward mailfile to “TO:my_real_internet_email@domain.com?Subject : ‘Pic script failure’ Subject

Remove mailfile from /home/{USER}/Maildir/new/
; cleanup; ; if no files were decocoded then
;we forwarded it elsewhere
;if files were decoded ok then all must have been ok
;so trash the original. Or maybe write it somewhere else for
;debugging

done

If someone could give me a hand coding this I would really appreciate it…. :slight_smile: I haven’t done any serious bash scripting since the early 90’s
I’m not stuck on doing it this way either. If someone knows something easier, better, etc by all means…suggest!.. Also if someone just knows something I should go read or study…. point me there… and I’ll go learn from it and carry on…. I Googled around for examples today and could find plenty for sending mail from a bash script, but nothing really at all for processing inbound mail to the shell.

Thanks in Advance!


#2

I thought you could upload images via mobile phone to ZenPhoto. Maybe it was Gallery2? I’m sure I’ve seen Scott mention uploading pics via mobile here on the board some months back.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#3

If you have web access on your phone, can you login to zenphoto using that and upload directly?

Other than that, I couldn’t find any tools right away for saving attachments other than metamail which isn’t installed by default. You’re probably better off using a library and scripting langauge, for example using Perl and the MIME-tools package I whipped up a script that processes one message at a time:

user@machine:~/test$ perl parse.pl ~/Maildir/new/x.y.machine.dreamhost.com Saving in /home/user/test/files Parsing /home/user/Maildir/new/x.y.machine.dreamhost.com Saved file attachment: Size: 301,570 bytes Name: tardis.wav Type: audio/wav Saved file attachment: Size: 421,390 bytes Name: 1217803520699.jpg Type: image/jpeg Saved file attachment: Size: 215,597 bytes Name: evil monkey.png Type: image/png
Customer since 2000 :cool: openvein.org


#4

thanks guys… i will study the perl idea… seems like it may be the best solution given what i saw at the link you provided. I’ve not done much with perl as yet, so this will be a great learning experience.

The point of emailing from the phone (vs uploading) is because of how easy it is… 3 clicks… click email pic, click the first contact on the list, click send. done.

With that in mind all of the pics do arrive as singles from the phone, but as I thought about how easy this is to upload pics I thought it would be nice to extend it since its such an easy interface to allow multiple pics because from the desktop or laptop it’s just plain convenient–and alot easier for my wife, she is a pro at emailing pics, but never seems to be able to upload or ftp without help.

I can kind of envision how to to this via bash but its indeed pretty intense uudecode appears to be available but I actually have no clue anymore how to parse the mailfile into distinct temp files by MIME sections.


#5

The basic perl script would be

[code]#!/usr/local/bin/perl

use MIME::Parser;

Where to store text and attachment files

my $output_dir = ‘/home/username/path’;

Get file argument

my $filepath = $ARGV[0] || die(‘no input file’);

my $parser = MIME::Parser->new();
$parser->output_dir($output_dir);
my $entity = $parser->parse_open($filepath);

exit;[/code]That might work for you but by default it will save the text parts to file too - not just attachments.

Customer since 2000 :cool: openvein.org


#6

I got that to work… THANKS!

i’ll post back my final solution here eventually… that got me past being dead stuck tho… :slight_smile: