Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

jdavidb
  (email not shown publicly)
http://voiceofjohn.blogspot.com/

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Friday December 20, 2002
10:14 AM

Writing a program to mail an Excel file as an attachment

[ #9572 ]

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;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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