    # sending mail, SMTP version
    # mail is piped back to postfix through a specific port
    # all SMTP methods return true (1) on success

    use Net::SMTP;
    my $SMTP_HANDLE = Net::SMTP->new("$localhost_ip:$smtp_port",
				Hello => "$localhost_name",
				Timeout => 30,
				Debug => 0
				);
    defined($SMTP_HANDLE) or die "Failure to connect to local SMTP port: $!";
    my($sender) = "<" . rfc2821_mailbox_addr($SENDER) . ">";
    if (!$SMTP_HANDLE->mail($sender)) {
	my($smtp_status) = $SMTP_HANDLE->status;
	my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	$SMTP_HANDLE->quit;
	if ($smtp_status == 5) {
	    do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	} else {
            die ( ($smtp_status == 4 ? "Temporary reject by MTA: "
				   : "Retry later, MTA said: ") . $smtp_msg);
	}
    }
    # The rfc2821_mailbox_addr() cleanup is necessary because addresses
    # we get from MTA are raw, with stripped-off quoting. To re-insert
    # them back via SMTP, the local-part needs to be quoted again
    # if it contains reserved characters or otherwise does not obey
    # the dot-atom syntax, as required per rfc2821. Failing to do that
    # gets us into trouble: amavis accepts message from MTA,
    # but is unable to hand it back to MTA after checking,
    # receiving '501 Bad address syntax' with every attempt.
    #
    my(@recips_2821) = map { "<".rfc2821_mailbox_addr($_).">" } @RECIPS;
    if (!$SMTP_HANDLE->recipient(@recips_2821)) {
	my($smtp_status) = $SMTP_HANDLE->status;
	my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	$SMTP_HANDLE->quit;
	if ($smtp_status == 5) {
	    do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	} else {
            die ( ($smtp_status == 4 ? "Temporary reject by MTA: "
				   : "Retry later, MTA said: ") . $smtp_msg);
	}
    }
    if (!$SMTP_HANDLE->data()) {
	my($smtp_status) = $SMTP_HANDLE->status;
	my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	$SMTP_HANDLE->quit;
	if ($smtp_status == 5) {
	    do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	} else {
            die ( ($smtp_status == 4 ? "Temporary reject by MTA: "
				   : "Retry later, MTA said: ") . $smtp_msg);
	}
    }
    my($skip_header_continuation) = 0;
    $fh->seek(0,0) or die "Can't rewind mail file: $!";
    while (<$fh>) {
	last if /^\r?\n$/;  # end-of-header reached
	if ($skip_header_continuation && /^[ \t]/) {
	    # discard
	} else {
	    $skip_header_continuation = 0;
	    $SMTP_HANDLE->datasend($_)
		or die "Net::SMTP::datasend failed: ".
			$SMTP_HANDLE->code() ." ". $SMTP_HANDLE->message();
	}
    }
    $_ = "";
    $_ .= "$X_HEADER_TAG: $X_HEADER_LINE\n" if $X_HEADER_LINE and
					       $X_HEADER_TAG =~ /^[!-9;-\176]+$/;
    $_ .= "\n";
    $SMTP_HANDLE->datasend($_)
	or die "Net::SMTP::datasend failed: ".
		$SMTP_HANDLE->code() ." ". $SMTP_HANDLE->message();
    for (;;) {
	$fh->read($_,16384);  # using fixed-size reads instead of line-by-line
			      # approach by <$fh>, makes feeding mail back to
	last if $_ eq '';     # Postfix more than twice as fast for larger mail
        if (!$SMTP_HANDLE->datasend($_)) {
	    my($smtp_status) = $SMTP_HANDLE->status;
	    my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	    $SMTP_HANDLE->quit;
	    if ($smtp_status == 5) {
		do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	    } else {
		die ( ($smtp_status == 4 ? "Temporary reject by MTA: "
				       : "Retry later, MTA said: ") . $smtp_msg);
	    }
	}
    }
    if (!$SMTP_HANDLE->dataend()) {
	my($smtp_status) = $SMTP_HANDLE->status;
	my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	$SMTP_HANDLE->quit;
	if ($smtp_status == 5) {
	    do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	} else {
	    die ( ($smtp_status == 4 ? "Temporary reject by MTA: "
				   : "Retry later, MTA said: ") . $smtp_msg);
	}
    }
    if (!$SMTP_HANDLE->quit) {
	my($smtp_status) = $SMTP_HANDLE->status;
	my($smtp_msg) = $SMTP_HANDLE->code ." ". $SMTP_HANDLE->message();
	if ($smtp_status == 5) {
	    do_log(0,"Rejected by MTA: $smtp_msg"); do_exit(1, __LINE__);
	} else {
	    die (($smtp_status == 4 ? "Temporary reject by MTA: "
				   : "Retry later, MTA said: ") . $smtp_msg);
	}
    }
    return 0;

# End postfix
