Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Unable to send the xls attachment file with the Mail::Sender

by vinoth.ree (Monsignor)
on Nov 21, 2011 at 04:55 UTC ( #939130=perlquestion: print w/replies, xml ) Need Help??
vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I have written the following Email sending program with attachment. When I run this program manually from my linux terminal Email goes with attachment. When I put this script in the crontab, Empty file is get attached, its happening only for this xls file, I am getting the HTML and TXT files

What could be the issue?

sub SendMail { my ($Html_File) = @_ ; my ($From_Addr, $To_Addr, $Cc_Addr, $Subject, $From_Address, $Msg_Bo +dy, $Email_From); $Email_From = $Sql_Globals::Email_From ; $From_Addr = $Sql_Globals::From_Addr ; $From_Address = "$Email_From <$From_Addr>"; $Subject = $Sql_Globals::Subject ; $To_Addr = $Sql_Globals::To_Addr ; $Cc_Addr = $Sql_Globals::Cc_Addr ; $Msg_Body = $Sql_Globals::Msg_body ; chomp($Subject); chomp($Cc_Addr); chomp($To_Addr); chomp($From_Address); &Connect_SMTP_Server; print "From_Address : $From_Address\n"; print "To_Address : $To_Addr\n"; print "Cc_Addr : $Cc_Addr\n"; print "Message : $Msg_Body\n"; print "File : $Html_File\n"; my $returncode = $main::SMTP_OBJ->MailFile({ from => qq($From_Address), #From address to => qq($To_Addr), #To address cc => qq($Cc_Addr), #Cc address subject => qq($Subject), #Subject of the Email msg => qq($Msg_Body), #Body message of the Email file => qq($Html_File) #Filename to attach }); print "Return Status : $returncode\n"; if ( ref($returncode) eq "Mail::Sender" ) { print "\n".localtime()."Email Sent.............\n"; &DisConnect_SMTPServer; } else{ print "\n".localtime()."Email Sending Failed.............\n"; print "$Mail::Sender::Error\n"; &DisConnect_SMTPServer; } } sub Connect_SMTP_Server { my $SMTP_Server_Details = &Get_SMTP_Details; my $SMTP_Obj; my($SMTP_ServerIP, $SMTP_Port, $SMTP_UserName,$SMTP_Password) = @$SM +TP_Server_Details; print "SMTP Server IP:$SMTP_ServerIP,Username:$SMTP_UserName,Passwor +d:$SMTP_Password\n"; ##SMTP server details unless(!$SMTP_ServerIP || !$SMTP_UserName ){ while(1){ ##Create object for Mail::Sender program $SMTP_Obj = Mail::Sender->new ( { 'smtp' => qq($SMTP_ServerIP), 'debug' => q(/tmp/Emaildebug.err), 'authid' => $SMTP_UserName, 'authpwd' => $SMTP_Password, }); ##Check connection succeed or failed if (ref ($SMTP_Obj) ne 'Mail::Sender'){ ##Log error message if Not able to connect to SMTP server print "\n".localtime()."CONNECT ERROR: Unable to connect to M +ailhost $SMTP_ServerIP"; print "\n".localtime()."..............Try Again after 15 seco +nds......."; sleep(15); } else{ print "\n"."=" x 80; print "\n".localtime()." : SMTP Server Connected Successfully. +......\n"; $main::SMTP_OBJ= $SMTP_Obj; print ">>>$main::SMTP_OBJ<<<\n"; last; } } } else{ print "SMTP Server details are not available."; print "Exiting the program........\n"; exit(0); } } unless(@ARGV){ print "\n\tGive File name as argument\n\n"; } else{ my $file = $ARGV[0]; print "$file"; &SendMail($file); }

Replies are listed 'Best First'.
Re: Unable to send the xls attachment file with the Mail::Sender
by mrguy123 (Hermit) on Nov 21, 2011 at 07:28 UTC
    Hi there, not sure exactly what your problem is yet, but a common error when running from crontab is that you don't give the full file path. In this case the file is sent to Sendmail from another place, so take a look at where it is coming from.
    Another thing that might help you is if you type "mail" in unix. This should give you a numbered list of all the cronjobs, for example:
    U 44 root@il-kblwe02.corp Sun Oct 30 12:14 29/1454 "Cron <guyn@il- +kblwe02> /bin/tcsh -c 'source /exlibris/sfx_ver/sfx4_1/guyn/home/.csh +" U 45 root@il-kblwe02.corp Sun Oct 30 12:27 29/1454 "Cron <guyn@il- +kblwe02> /bin/tcsh -c 'source /exlibris/sfx_ver/sfx4_1/guyn/home/.csh +" >N 46 root@il-kblwe02.corp Mon Oct 31 12:27 35/1699 "Cron <guyn@il- +kblwe02> /bin/tcsh -c 'source /exlibris/sfx_ver/sfx4_1/guyn/home/.csh +" N 47 root@il-kblwe02.corp Tue Nov 1 12:27 35/1696 "Cron <guyn@il- +kblwe02> /bin/tcsh -c 'source /exlibris/sfx_ver/sfx4_1/guyn/home/.csh +"
    By typing the number of the cron job you can get details about it which will help you debug
    Good luck!!

      Actually I am calling a perl program only from the crontab. In that program I have another perl program which will send the mail, for that mail sending program I am giving the full path of the file to be attached with the mail. I am executing the mail sending program using the qx($Cmd).

        Hi, can you maybe print a small standalone program that recreates your bug? It will make it much easier to understand what might be the problem
Re: Unable to send the xls attachment file with the Mail::Sender
by jmcnamara (Monsignor) on Nov 21, 2011 at 13:25 UTC

    If you are using Spreadsheet::WriteExcel to generate the XLS file then you need to ensure that you close() the workbook prior to attaching it to an email. From the docs:

    In general your Excel file will be closed automatically when your program ends or when the Workbook object goes out of scope, however the close() method can be used to explicitly close an Excel file. ... An explicit close() is required if the file must be closed prior to performing some external action on it such as copying it, reading its size or attaching it to an email.


      Hi John, that's a good call. Had the same problem myself a few months ago
      I hope this solves Ree's problem

      Thanks jmcnamara ++

      I am using the Spreadsheet::WriteExcel perl module to generate the xls file. I also checked the code and found that I did not close that file. Once I generate this xls file I am sending this xls file name for EmailSending program. Now I modified the code let me check and confirm.


      Its working fine! thanks a lot

      thanks ... :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://939130]
Approved by GrandFather
LanX what?
marto deletes himself from the all lists
QM deletes himself from all delete lists
LanX passes the blame to jdpatches. ..
Eily the verb from this sentence
[LanX]: jdporter adjusted the best nodes algorithm not long ago to public demands, my theory more complicated= more error prone

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (11)
As of 2018-03-20 10:14 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (249 votes). Check out past polls.