cc has asked for the wisdom of the Perl Monks concerning the following question:

hi

this perl script should be scheduled every 1 minute.
it should look for a doc file and send it with the original name as attachment by mail.
every doc file has other name.
after the file was sent, it should create a backup direktory and move the file there.

I have 3 problems:
1.) my first problem is:
/srv/ftp/lc/*.DOC: not readable

2.) I'll send a file with a original name
Filename =>'?',

3.) sometimes I'm getting 3 or more files at the same time , but must send only one file in one mail.
I don't know, how can I change that.
#!/usr/bin/perl -w use strict; use warnings; use MIME::Lite; use Net::SMTP; my $mail_host = "192.168.1.1"; my $linux = "root\@mydomain.net"; my $recipient = "recipient\@mydomain.net"; my $file = "/srv/ftp/lc/*.DOC"; my $subject = "invoice"; # send a mail with the attachment my $msg = MIME::Lite->new( From => "$linux" , To => "$recipient" , Subject => "$subject", Type => 'multipart/mixed', ); $msg->attach(Type =>'TEXT', Data =>"invoice" ); $msg->attach(Type =>'application/doc', Path =>"$file", Filename =>'?', Disposition => 'attachment' ); # ----- Tell MIME::Lite to use Net::SMTP instead of sendmail MIME::Lite->send('smtp', $mail_host, Timeout=>60); $msg->send; # create a backup subfolder after mail was sent my @dt = localtime; my $subfolder_name = ((((1900 + $dt[5]) * 100 + 1 + $dt[4]) * 100 + $d +t[3]) * 100 + $dt[2]) * 100 + $dt[1]; mkdir "/srv/ftp/lc/save/$subfolder_name" or die "$subfolder_name: $!"; # move the file to the backup subfolder system("mv $file /srv/ftp/lc/save/$subfolder_name"); exit;

could someone help me pls ?

Replies are listed 'Best First'.
Re: look for a file and send as a mail attachment by mail
by roboticus (Chancellor) on May 31, 2006 at 02:21 UTC
    cc:

    Heh...I wrote nearly that *exact* program a couple of years ago for work. I'd post it, except I wrote it in C#. Having said that, here are a couple of notes on your problems:

    1) File not readable: I experienced the same thing. The problem is that the Windows app still had the file open. Once the file was closed, my app would read, send and delete it. It could also be permissions/ACL settings too. What I did here was to make a note of the file (push it onto an array), and try again on the next one-minute tick. Quite often, the file would be on the list for 15 minutes or so.

    2) Need unique file names: Read the perldoc for File::Temp.... 8^).

    3) Handle multiple files, but send only one mail per file: Turn your script into a subroutine, and call it once for each filename.

    Regarding the code you posted: it looks like a good start, but one thing I'd change is that I'd not create so many subfolders, lest you make directory access incredibly slow after a while. (Many filesystems get slow if you have too many items in a directory.)

    If you really need to segregate your files into subdirectories in a one-minute resolution, then you might want to split the hierarchy, like use YYYYMMDD/HHMM, so you'll only have 1440 items max in each YYYYMMDD directory.

    Finally, you may want to check for the existence of the directory before calling mkdir so you don't die when you try to create a directory that already exists.

    If you have any more questions, let us know!

    --roboticus

      It sounds like you need to use File::List in order to get a list of files in the directory. Once you have this, then you can use the $msg->attach call any number of times to add the files to your message. Hope this helps: but File::List is really easy to use, and it won't require too much re-work of your script.