# BEGIN_MTA_SEND
# MTA: qmail
# sending mail

	# In the case of qmail, we pass the original data back to
	# qmail-queue-real.  It reads the email on descriptor 0, and the
	# envelope on descriptor 1.

	pipe(MESSAGE_READER, MESSAGE_WRITER)
	  or die "Cannot open message pipe: $!";
	pipe(ENVELOPE_READER, ENVELOPE_WRITER)
	  or die "Cannot open envelope pipe: $!";

	defined(my $child = fork) or die "Cannot fork: $!";

	if ($child) {
	    # Parent
	    my $oldfh = select(MESSAGE_WRITER); $| = 1; 
			select(ENVELOPE_WRITER); $| = 1;
	    select($oldfh);

	    close MESSAGE_READER  or die "Close msg reader fault: $!";
	    close ENVELOPE_READER or die "Close envelope reader fault: $!";

	    while (<$msgfh>) {
		next if ($seen_xheader == 0 && m/^$X_HEADER_TAG:/o);
		if ($seen_xheader == 0 && m/\A\r?\n\Z/) {
		    print MESSAGE_WRITER "$X_HEADER_TAG: " . "$X_HEADER_LINE\n";
		    $seen_xheader = 1;
		}
		print MESSAGE_WRITER $_;
	    }

	    close MESSAGE_WRITER or die "Close msg writer fault: $!";

	    $SENDER = "" if $SENDER eq "<>";
	    print ENVELOPE_WRITER "F$SENDER\0" . join("", map { "T$_\0" } @RECIPS) . "\0"
	      or die "Cannot write envelope: $!";

	    close ENVELOPE_WRITER or die "Close envelope writer fault: $!";
	    waitpid($child, 0);
	    my $exit_code = $? >> 8;
	    $exit_code and die "Unable to queue message ($exit_code): $!";
	} elsif (defined $child) {
	    # Child
	    close MESSAGE_WRITER
	      or die "Cannot close message writer in parent: $!";
	    close ENVELOPE_WRITER
	      or die "Cannot close envelope writer in parent: $!";

	    open(STDIN, "<&MESSAGE_READER") or die "b1";
	    open(STDOUT, "<&ENVELOPE_READER") or die "b2";

	    unless (exec("$QMAILDIR/qmail-queue-real")) {
		die "should never be here!";
	    }
	}
# END_MTA_SEND
