This post originated from an RSS feed registered with Web Buzz
by Douglas Clifton.
Original Post: Sendmail in Perl
Feed Title: blogZero
Feed URL: http://loadaveragezero.com/app/s9y/index.php?/feeds/index.rss1
Feed Description: Web Development News, Culture and Opinion
People love to argue. A recent thread on the DCPerlMongersmailing list opened yet another discussion of which CPAN module is the best for sending email.
Most Unix systems, at least the open-source flavors I use most frequently (FreeBSD and Linux), have sendmail installed. So when I need to send an email from a Perl script I just roll my own code to do it. You could argue that it's too simplistic because it's procedural or it isn't portable, but it works, it's fast and I have control over the code.
The problem with using someone else's module is you're tied into the way they think, and it can be difficult or time consuming to modify the code to add a feature or fix a bug.
Who made this rule that you have to use a module for everything or that object-oriented methods are somehow intrinsically superior to procedural programming? Don't get the wrong idea, I have nothing against modules or OOP. It would have taken me a hell of a lot longer to write many of the Perl scripts I've written without wonderful tools like CGI.pm and DBI. Not to mention about a 100 others.
#!/usr/bin/perl
$to = 'you@somehost.com';
$from = 'me@somehost.com';
$subject = 'test sendmail';
$body = <<EOB;
Hello from Perl/sendmail
EOB
if ($error = sendmail($to, $from, $subject, $body)) {
die "Can't sendmail: $error\n";
}
print 'mail sent';
sub sendmail($$$$) {
my @args = (
'to',
'from',
'subject',
'body'
);
my $arg;
foreach $arg (@args) {
unless ($$arg = shift) {
return (caller(0))[3] . ': missing $' . $arg . ' parameter';
}
}
my $sendmail = '/usr/sbin/sendmail';
my $switch = '-t';
open MAIL, "|$sendmail $switch" or return $!;
print MAIL <<EOM;
To: $to
From: $from
Subject: $subject
$body
EOM
close MAIL;
return;
} # sendmail()