#!/usr/bin/perl -w -I/var/tmp/perl-root/usr/lib/perl5/site_perl
# A perl program to send email by command line. Up to five attachments.
# Usage: "mailerX ToEmailAddress MessageBodyFile [Attachment1] [Attachment2] [Attachment3] [Attachment4] [Attachment5] "

use MIME::QuotedPrint;
use MIME::Base64;
use MAIL::Sendmail; # 0.75; # doesn't work with v. 0.74!

$ToEmailAddress=$ARGV[0];
$MessageBodyFile=$ARGV[1];
$Attachment1=$ARGV[2];
$Attachment2=$ARGV[3];
$Attachment3=$ARGV[4];
$Attachment4=$ARGV[5];
$Attachment5=$ARGV[6];


%mail = (
        SMTP => 'mail.teleport.com',
        from => 'THUNDERCHIEF! <renegade@teleport.com>',
        to => "$ToEmailAddress",
        subject => 'Renegade auto-mailer',
        'X-Mailer' => "Mail::Sendmail version $Mail::Sendmail::VERSION",
        );
        

$boundary = "====" . time() . "====";
$mail{'content-type'} = "multipart/mixed; boundary=\"$boundary\"";

$message = encode_qp( "Voilà le fichier demandé" );

$file = $^O; # This is the perl executable

open (F, $file) or die "Cannot read $file: $!";
binmode F; undef $/;
$mail{body} = encode_base64(<F>);
close F;

$boundary = '--'.$boundary;
$mail{body} = <<END_OF_BODY;
$boundary
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

$message
$boundary
Content-Type: application/octet-stream; name="$^X"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$^X"

$mail{body}
$boundary--
END_OF_BODY

sendmail(%mail) || print "Error: $Mail::Sendmail::error\n";






