View Single Post
Old Sep 19th, 2006, 12:30 PM   #1
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Mail::Sender Problems

Hey,

So i have been working on a fax server for my company, so we can get rid of some of huge amounts of paper around here. I have hylafax setup and working perfectly. I have written a perl script that uses OCR software to read the incomming cover sheets and email the respective user on the cover sheet a pdf version of their fax (ofcourse if the cover sheet is has hand writing on it instead of text the OCR part doesn't work and it emails it to a default user for routing).

Anyways, i pretty much have it working. However when it goes to send the email (with the fax attached), Mail:ender gives me the error:

Quote:
Originally Posted by Mail::Sender
File "File" not found
It's caused by this line:
(ref ($sender->MailFile(to => $default_mail, subject => 'New Fax!', msg => 'Please see attached file for your fax.', File => $file)) and print "Mail Sent OK!") or die $Mail::Sender::Error, "\n";
here's the whole script:
#!/usr/bin/env perl
#
#########################################################################################
## Purpose: This script handles all the routing and emailing of faxes that are recived.##
## Author: Pizentios                                       ##
## Date Created: 2006-09-12                                   ##
## File Name: fax-route.pl                                   ##
#########################################################################################


use Pg;
use Mail::Sender;
use File::Copy;

#log searching patterns. 
my $log_patterns = "RECV FAX: bin\/faxrcvd";
#location of where the faxes get stored. 
my $fax_location = "/var/spool/fax/recvq";
#location of work dir.
my $work_dir = "/root/scripts";
#file name for OCR output.
my $output_name = "output";
my $pass = 1;
#mail settings:
my $smtp_host = "XXXXXXXX";
my $from = "XXXXXXXX";
my $reply_address = "XXXXXX";
my $default_mail = "XXXXXXXX";

#db connection stuff:
##$con_val[0] == the database name.
##$con_val[1] == the host.
##$con_val[2] == the port.
##$con_val[3] == the username for the database.
##$con_val[4] == the password for the database.
my @conn_val = ("fax", "XXXXXX", "XXXX", "XXXXXX", "XXXXX");

#SMTP mail stuff:
my $sender = new Mail::Sender (smtp => $smtp_host, from => $from, replyto => $reply_address);

#connect up to the db.
my $conn = Pg::connectdb("host=" . $conn_val[1] . " port=" . $conn_val[2] . " dbname=" . $conn_val[0] . " user=" . $conn_val[3] . " password=" . $conn_val[4]);
##check connection.
if ($conn->status != PGRES_CONNECTION_OK)
{
        die "Failed to connect: ".-$conn->errorMessage."\n";
}

#monitor the logs to see when a fax is comming in. 
open (TAIL, "tail -f /var/log/messages 2>&1 |") or die "can't open pipe:$!";

while(<TAIL>)
{
    #loop and watch the log all the time. 
    if ($_ =~ m/$log_patterns/)
    {
        #returns true if we have a match on $_
        #now we need to grab the file name of the new fax.
        $_ =~ m/fax[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].tif/;
        $file_name = $&;
        copy("$fax_location/$file_name", "$work_dir/$file_name") or die "Copy Failed....";
        @args = ("tesseract", "$work_dir/$file_name", "$work_dir/$output_name");
        system(@args); # or die "OCR Software Failed..."; #run OCR software and dump output to a file. 
        #it quits here. 
        print "Test2\n";
        open (FAX, "$work_dir/$output_name.txt") or die "Couldn't open OCR output file..."; #open OCR output file.
        my @lines = <FAX>; #dump OCR output into an array.
        close (FAX); #close the OCR output file. 
        print "Test3\n";
        my $result = $conn->exec("SELECT * FROM rules"); #grab our search rules from the db.
        foreach $line (@lines)
        {
            print "Test4\n";
            #loop through the lines of the fax cover sheet.
            while (@row = $result->fetchrow)
            {
                print "Test5\n";
                #loop through the search patterns from the database and see if anything matches.
                #TODO: Fix regex matching here to enable emailing to respective user. 
                if ($line =~ m/$row[1]/)
                {
                    print "Test6\n";
                    #if we have a match set pass var and exit loops.
                    #0 = true;
                    $effected_users = $row[2];
                    $pass = 0;
                }
            }
            if ($pass == 0)
            {
                #we had a match, exit loops.
                last;
            }
        }
        #make the new file name:
        $old_file_name = $file_name;
        $file_name =~ s/.tif/.pdf/g;
        #convert the fax .tif file to pdf format for easy viewing.
        system ("convert $work_dir\/$old_file_name $work_dir\/$file_name");# or die "Convertion of fax .tif to .pdf failed...";
        if ($pass == 0)
        {
            print "Test1\n";
            #grab effected users from the database. 
            my @users = split(/,/, $effected_users);
            foreach $user (@users)
            {
                if ($user != "")
                {
                    $result = $conn->exec("SELCT * FROM users WHERE idx=$user");
                    @row = $result->fetchrow;
                    $To .= $row[3] . ",";
                }
            }
            chop $To;
            #mail to the matched users.
            print "Test7\n";
            (ref ($sender->MailFile(to => $To, subject => 'New Fax!', msg => 'Please see attached file for your fax.', file => "$work_dir\/$file_name")) and print "Mail Sent OK!") or die $Mail::Sender::Error, "\n"; 
        }
        else
        {
            print "Test8\n";
            #mail to default fax user (the user that directs all non-match faxes)
            my $file = "$work_dir\/$file_name";
            (ref ($sender->MailFile(to => $default_mail, subject => 'New Fax!', msg => 'Please see attached file for your fax.', File => $file)) and print "Mail Sent OK!") or die $Mail::Sender::Error, "\n";
        }

    }
}
Anyways, i don't understand why it can't find the file. I have tried several methods of passing the location of the file to the Mail:ender object, but all have failed. Do any of you guys have any ideas?

Thanks in advance.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote