Important note, this package doesn't function as well as SSMTP which is also a null mail client. It is a more complete and compatible replacement for sendmail on machines that don't recieve any mail.
This is not a full blown replacement for sendmail, rather it is a limited replacement for use in certian cicumstances. It does not understand CC or BCC, or much else really. It will block until the message has been dropped onto the receiving server. Not suitable for use with large mail volumes.
This reason this script exists is to allow machines that only send mail from daemons, like cron, to communicate. Why install a full on mail package when something this simple would do?
#!/usr/bin/perl -w
# David Busby - Edoceo, Inc.
use strict;
use Data::Dumper;
use Net::SMTP;
use Net::DNS;
# Tell me who root is
use constant ROOT_IS => 'your_email@your_domain.tld';
# Supposed to be for retry count, not implemented
use constant C_RETRY => 0;
# You've seen something like this before
use constant DEBUG => 0;
$!=1;
# Do you want to log what happens?
open (STDOUT,'>>/tmp/sendmail.log');
open (STDERR,'>&STDOUT');
my $buf = do { local ($/); <STDIN>; };
my %cfg;
my $cmdline = join(' ',@ARGV);
if (DEBUG) { print "CMDLINE: $cmdline\n"; }
# BUG?: This only takes simple email addresses, not fancy ones
if ($cmdline =~ m/-f *([\w\.]+@[\w\-\.]+)/o) { $cfg{sender} = $1; }
if ($cmdline =~ m/-F *(\w+)/o) { $cfg{full_name} = $1; }
# Parse To Address Header
my $to = '';
if ($buf =~ m/To: (.+)/)
{
if ($1 eq 'root') { $to = ROOT_IS; }
else { $to = $1; }
}
if (DEBUG) { print "TO: $to\n"; }
if (!$to) { print STDERR "FATAL: No `To:` address specified\n"; exit(1); }
my ($user,$host) = $to =~ m/(.+)@(.+)/;
# My Hostname
my $host_name = do { my $x = `/bin/hostname -f`; chomp($x); $x; };
my $domain_name = do { my $x = `/bin/hostname -d`; chomp($x); $x; };
# Resolve name
my $res = Net::DNS::Resolver->new(debug => DEBUG);
my @mx = mx($res,$host);
if (@mx)
{
foreach my $rr (@mx)
{
if (DEBUG) { print "MX: ".$rr->preference.' '.$rr->exchange."\n"; }
# Send
# BUG: No error checking or recovery
my $smtp = Net::SMTP->new($rr->exchange, Hello => $host_name, Debug => DEBUG);
$smtp->mail('cron@'.$domain_name);
$smtp->to($to);
$smtp->data($buf);
$smtp->quit();
last;
}
}
else
{
if (DEBUG) { print "No MX for $host\n"; }
}
exit(0);
No man page, no make install, just download this script to /usr/sbin and you're ready to rock.
wget -O /usr/sbin/sendmail http://www.edoceo.com/dl/sendmail.pl chmod 0755 /usr/sbin/sendmail
Run this simple command:
rm `which sendmail`