Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach xlsx to email with MIME::Lite

I am trying to send a email with an attached xlsx file using Excel::Writer::XLSX and MIME::Lite. The generation of the excel file works, as i can scp and open the file in Excel without any problems. When i try to open the attachment from my email client (Outlook 2013) i get this error:

"Excel cannot open the file "from_2014-06_to_2014-07.xlsx" because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

The file size that outlook displays is 444B but its actually 95K. I have been sending xls files using Spreadsheet::WriteExcel and mime type "application/vnd.ms-excel" without any problems before.

Here is what I have tried to send the email:

sub send_mail{
    my $filename = shift;
    my $to_email = shift;
    my $from_email = shift;
    my $date = shift;
    $filename = shift;

    my $mail = MIME::Lite->new(
        'From'          => '$from_email',
        'To'            => $to_email,
        'Subject'       => "Radio/TV stats $date",
        'Type'          => 'multipart/mixed',
        #'content-type'  => 'application/zip',
        #'Data'       => "Here is your stuff",
    );

    $mail->attach(
        'Type'          => 'TEXT',
        'Data'          => "Here is your stuff",
    );

    $mail->attach(
        #'Type'          => 'application/vnd.ms-excel',
        'Type'          => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'Path'          => $filepath,
        'Filename'      => $filename,
        'Disposition'   => 'attachement',
    );

    $mail->send('sendmail');
}

Can anyone please help my attach the xlsx file?

like image 603
Twistar Avatar asked Dec 11 '25 10:12

Twistar


1 Answers

You shift twice to $filename (2nd and 6th strings) and variable $filepath is not declared. May be here error?

like image 110
Sergey Svistunov Avatar answered Dec 14 '25 09:12

Sergey Svistunov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!