http://www.perlmonks.org?node_id=36579


in reply to sending email

I find Mail::Sendmail to be easy, reliable and cross-platform.

However, also note that sending mail with attachments is really easy with MIME::Lite (although you need to install Net::SMTP to use this, as other folks have noted). MIME::Lite is available through Activestate's PPM, which makes life easier.

Check out this simple cross-platform (tested on Linux and NT). NT gives an annoying: "The system cannot find the path specified" error, but still works.

There are, of course, lots of ways to improve it (use a 'magic' file to discern the MIME type, use Getopt::Long for a more coherent command line, allow the user to set more parameters, etc.)

#!/usr/bin/perl use strict; use MIME::Lite; my $DEBUG = 0; my %TYPES = ( csv => [ 'text/csv', '8bit' ], gif => [ 'image/gif', 'base64' ], tiff => [ 'image/tiff', 'base64' ], tif => [ 'image/tiff', 'base64' ], jpeg => [ 'image/jpeg', 'base64' ], jpg => [ 'image/jpeg', 'base64' ], zip => [ 'application/zip', 'base64' ], gz => [ 'application/gzip', 'base64' ], html => [ 'text/html', '8bit' ], htm => [ 'text/html', '8bit' ], pdf => [ 'application/pdf', 'base64' ], ); { my $subject = shift; my $filetype = shift; my $filename = shift; my $email = shift; die "Usage: $0 subject file-type filename email\n" unless ( $email ) +; my %mail = ( subject => $subject, to => $email, from => 'webmaster@in +tes.net' ); my $msg = new MIME::Lite( From => $mail{from}, To => $mail{to}, Subject => $mail{subject}, Data => 'Emailing file; should be attached' +, Type => 'text/plain' ); my $type = $TYPES{ lc $filetype } || [ 'text/plain', '8bit' ]; $msg->attach( Type => $type->[0], Encoding => $type->[1], Path => $filename ); MIME::Lite->send( 'smtp', 'localhost', Timeout => 20 ); $msg->send || die "Cannot send message: $!"; }