Writing a program to mail an Excel file as an attachment

jdavidb on 2002-12-20T15:14:02

Sometimes modules just come together in such a way as to make my life very easy and enable me to dazzle the people I work for. In this case, simplified APIs to more complicated functionality: MIME::Lite and Spreadsheet::WriteExcel::Simple. Here's an example of how to create and mail a simple spreadsheet as an attachment. For bonus points, generalize it to get the data from DBI or a Data::Table object.

#!/usr/local/bin/perl5.8.0 -- # -*- perl -*-

use warnings;
use strict;

use Spreadsheet::WriteExcel::Simple;
use MIME::Lite;

my $xls = Spreadsheet::WriteExcel::Simple->new;
$xls->write_bold_row([qw(col1 col2 col3)]);
for (1 .. 5)
{
    $xls->write_row([qw(one two three)]);
}

my $msg = MIME::Lite->new(
			  From => 'me@example.com',
			  To => 'you@example.com',
			  Subject => 'Example: mailing an Excel workbook',
			  Type => 'TEXT',
			  Data => <<'EOF',
Here is your example Excel workbook.
EOF
			  );
$msg->attach(
	     Type => 'application/vnd.ms-excel',
	     Data => $xls->data,
	     Filename => 'example.xls',
	     );
$msg->send;


Mailing attachments

gnat on 2002-12-27T15:49:39

Yes, I wrote a very simple MIME-Lite script that mails files to people as attachments. It's so much simpler than fiddling around in my mailer, and I can pipe to it. (MIME-Lite)++

--Nat