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

Hey, guys!

I've been fussing with this for awhile & thought I'd ask those that know more than me. I've been using MIME::Lite::TT for automating email messages with success, but now need to add a simple text attachment. After poking about the Internet, I've cobbled the following together which sends the attachment correctly, but now I'm losing the text of the message itself:

use strict; use warnings; use MIME::Lite::TT; use Data::Dumper; send_email(); sub send_email { my $options = { INCLUDE_PATH => '.' }; my $msg = new MIME::Lite::TT( From => 'me@xyzzy.com', To => 'me@xyzzy.com', Subject => 'test message', Type => 'multipart/mixed', Template => 'template.txt', TmplOptions => $options, TmplParams => {} ); # $msg->attr('content-type' => 'multipart/mixed'); $msg->attach( Type => 'text/plain', Path => 'test.pl', Filename => 'attachment.txt', Disposition => 'attachment' ); $msg->send(); }

Replies are listed 'Best First'.
Re: attachments with MIME::Lite::TT?
by Khen1950fx (Canon) on Mar 03, 2012 at 10:44 UTC
    FWIW, the Template option in the constructor should be a sref; TmplOptions and TmplParams should both be a href. Here's what I played with. Don't forget your template:-).
    #!/usr/bin/perl BEGIN { $MIME::Lite::VANILLA = 1; } use strict; use warnings; use MIME::Lite::TT; use Data::Dumper::Concise; my $template = <<TEMPLATE; This is a template. my name is [% me %]. TEMPLATE my %params = (name => 'me'); my %options = (EVAL_PERL => 1); send_email(); sub send_email { my $msg = MIME::Lite::TT->new( From => 'me@xyzzy.com', To => 'me@xyzzy.com', Subject => 'test message', Type => 'multipart/mixed', Template => \$template, TmplOptions => \%options, TmplParams => \%params, ); $msg->attach( Type => 'AUTO', Path => '/tmp/test.pl', Readnow => 1, Filename => 'test.pl', ); $msg->stringify; $msg->print(\*STDOUT); } exit; __END__
    And a very simple test.pl to put in /tmp
    #!/usr/bin/perl # test.pl use strict; use warnings; print "This is a test. Please standby for instructions\n";