#! /usr/bin/perl -w
# Send a mail message by dropping it directly into the user's ~/Maildir/new.

use strict;

if ( $#ARGV != 0 && $#ARGV != 1 && $#ARGV != 2 )
   {
   die "Usage: tmda-email-maildrop [-html] From [Subject]\n";
   }

my $html = 0;

if ( $ARGV[0] eq "-html" )
   {
   $html = 1;
   shift;
   }

my ($User,$pass,$uid,$gid,$quota,$comment,$gcos,$dir,$shell,$expire) =
   getpwnam( $ENV{"USER"} );
my $mail_from = shift || die "From field not specified.\n";
my $subject = shift || "(none)";
my $FullName = $gcos;
my $HostName = `hostname`;
chomp $HostName;
my $date = `date '+%a, %d %b %Y %H:%M:%S %z'`;
chomp $date;
my $mail_to = "$User\@$HostName";
my $mime_boundary; # = "---TMDA-PENDING-SUMMARY---";

# Set up for dropping mail file into user's qmail Maildir INBOX.
my $MailDir = $ENV{"HOME"} . "/Maildir";
my $TmpDir = "$MailDir/tmp";
my $NewDir = "$MailDir/new";
my $TmpExt = time . ".$$." . $HostName;
my $TmpFile = "$TmpDir/$TmpExt";
my $NewFile = "$NewDir/$TmpExt";

my $msg_id;
my $desc;

open( OUT, ">$TmpFile" ) or die "Can't open $TmpFile\n";
print OUT "From: ", $mail_from, "\n";
print OUT "To: ", $mail_to, "\n";
print OUT "Date: $date\n";
print OUT "Subject: ", $subject, "\n";
print OUT "Mime-Version: 1.0\n" if ( $html );
if ( $mime_boundary && $html )
   {
   print OUT "Content-Type: multipart/alternative; boundary=\"",
      $mime_boundary, "\"\n";
   }

print OUT "Content-Type: text/html; charset=\"iso-8859-1\"\n" if ( $html );
#print OUT "Content-Transfer-Encoding: quoted-printable\n";
print OUT "\n";

if ( $mime_boundary && $html )
   {
   print OUT $mime_boundary, "\n";
   }

while ( <> )
   {
   print OUT;
   }

if ( $mime_boundary && $html )
   {
   print OUT "$mime_boundary\n";
   }

close( OUT );

rename( $TmpFile, $NewFile );

exit 0;
