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

###################################################################### +##### # In: # Out: # Works but no file attached. sub mailfile {my($replyaddr,$fnout,$subjparam)=@_; # mailsender() my(@a,@b,$i,$j,$k,$procname,$result,$s,$t); my(%data,$body,$divline,$fn,$hashcnt,$key,$err,$sender,$to); my($prefixsp); $procname="mailfile"; $prefixsp=' '; # Prefix spaces to indent msgs nicely. $to=$replyaddr; # Send email to this address. if (len($to)<3) # Email must be at least n ctrs. { $s=$prefixsp."$procname ERROR: To address is missing or invali +d."; writeerr($s); exit 10; } $divline="========================================\r"; $s=$prefixsp."$procname: Going to create Mail::Sender "; if (len($fnout)>0) { $s.="with file $fnout"; } $s.="..."; writeerr("$s"); # DEBUG if (! -e $fnout) { $s="$procname ERROR: $fnout does not exist"; writeerr($s); exit 1; } $sender=new Mail::Sender(); # Line feeds for Outlook 2012 require \r. $body=''; $body.="Log follows. Look for lines that say ERROR.\r"; $body.=$divline; $body.="\r.\r"; # Required for email. Period on line by itself. if (len($fnout)>0) { if (-e $fnout) # If $fnout exists then... { @a=split('/',$fnout); $fn=$a[$#a]; # Drop directories from $fnout. $s=$prefixsp."$procname: Attaching output file: $fnout"; # + DEBUG writeerr($s); $result=0; # Was $result=$sender->MailFile(...) $result=$sender->MailFile( { file=>$fnout, client=>'xxxclient', smtp=>$server, port=>80, from=>$fromaddr, replyto=>$fromaddr, authid=>$authuser, authpwd=>$authpw, auth=>'LOGIN', debug=>'debugt.txt', ctype=>'application/vnd.ms-excel', multipart=>'multipart/mixed', encoding=>'base64', disposition=>"attachment; filename=$fnout", to=>$to, subject=>$subjparam, msg=>$body }) and print $prefixsp."$procname: Mail sent ok\n"; if (($result<0) ) { $s=$prefixsp."$procname ERROR: ". "$result on MailFile(): '". trim($Mail::Sender::Error)."'\n"; writeerr($s); $sender->Close(); exit 1; } } # if -e fnout else { $s=$prefixsp."$procname ERROR: $fnout does not exist."; writeerr($s); $sender->Close(); exit 1; } } else { print "Filename $fnout was blank\n"; } # else # Now close connection. $sender->Close(); if ($@) { $s=$prefixsp."$procname ERROR: Could not make new Mail::Sender +. $@"; writeerr($s); exit 1; } else { $s=$prefixsp."$procname: Sent email to $to"; if (len($fnout)>0) { $s.="\n".$prefixsp." with file $fnout"; } writeerr($s); } writeerrfile('tmailsend'); return; # mailfile }
Thank you!

Perl 5.8.8 on Redhat Linux RHEL 5.5.56 (64-bit)

Replies are listed 'Best First'.
Re: Mail::Sender not attaching XLS file
by zentara (Cardinal) on Jul 15, 2014 at 13:22 UTC
    Have you tried the ultimate in simplicity, the complexity of your script dissuades me from trying to figure out WTF is going on. :-)?
    #!/usr/bin/perl use Mail::Sender; my $sender = Mail::Sender->new(smtp => $mailserver, from => $mailfrom); $sender->MailFile( {to => $mailto, subject => 'Daily report file', file => $outfile} );

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Mail::Sender not attaching XLS file
by roboticus (Chancellor) on Jul 15, 2014 at 12:34 UTC

    bulrush:

    Have you verified that other EMail receiving programs also don't show the attachment?

    I'd suggest two things:

    1. Send an EMail with an attached spreadsheet using a method that works well, and then compare the EMail with the one that doesn't seem to work. There could be a difference that gives you a hint.
    2. Send the EMail to a gmail account or use a different mail client to view your EMail to see if the attachment shows up.

    I haven't worked with EMail in some time, but I recall having a few difficulties with EMail attachments, too. I don't recall what the problem was, but it was something not particularly obvious at the time. (But it wasn't complicated, either.)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.