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

All,

I've found some threads on how to attach a binary file to a email, but I'm having some problems trying to figure out how to attach a xls (Excel) file. Here is the snippet of code that I'm struggling with:

open (XLS, $file) or die "Can't open file: $file -- $!\n"; undef $/; $message1 = (<XLS>); %emailhash = ( 'to' => "toname\@company.com", 'from' => "fromname\@company.com", 'subject' => $subject2, # 'message' => $message1, 'disposition' => $file ); sendmail(%emailhash);
if I "unpound" the 'message' key, I see the Excel output in the email itself, but it's not showing up as attached.

Any help would be appreciated.

Replies are listed 'Best First'.
Re: Using Sendmail and attaching a xls (Excel) file
by chas (Priest) on Apr 18, 2005 at 23:32 UTC
    You didn't say which module you are using, but it looks like what you are doing is just including the file as a message body, not as an attachment. Check out some of the MIME-Tools modules here.(e.g. MIME::Entity)
    chas
Re: Using Sendmail and attaching a xls (Excel) file
by zentara (Cardinal) on Apr 19, 2005 at 12:37 UTC
    Here is an example using a module to handle the MIME type:
    #!/usr/bin/perl use MIME::Entity; use Net::SMTP; # create the multipart message with attachments $msg = MIME::Entity->build( Type => 'multipart/mixed', From => 'bogus@mail.com', # The "To" here is displayed in the message header as "To" # This is not the actual list of recipients To => '"Display Name" <user1@mail.com>, "Second Person" <user2@mail.com>', Subject => 'Automatic email of Excel report', ); $msg->attach( Type => 'application/msexcel', Path => $report, Filename => 'college_orders.xls', Encoding => 'base64' ); $msg->attach( Data => "Enclosed is the daily report of orders. (automated delivery)" ); # send the message $smtp = Net::SMTP->new("smtp.mail.com"); #authenticate if required $smtp->auth( "login", "passwd" ); # Identify yourself to the smtp server $smtp->mail('bogus@mail.com'); # The syntax for "To" is different in MIME::Entity and Net::SMTP # The "to" here is the list of actual recipients and is not displayed $smtp->to( 'user1@mail.com', 'user2@mail.com' ) || die "Bad address"; # Send the message and attachments $smtp->data( [ $msg->as_string ] ) || die "mail not accepted"; $smtp->quit; exit;

    And since you are using sendmail in your question, here is how to do attachments with sendmail manually. You may have to adjust the content type for .xls (I never use it so I'm guessing). You can see, modules make it alot easier, setting up the multipart boundaries for you.


    I'm not really a human, but I play one on earth. flash japh