The Artima Developer Community
Sponsored Link

Web Buzz Forum
Sendmail in Perl

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Douglas Clifton

Posts: 861
Nickname: dwclifton
Registered: May, 2005

Douglas Clifton is a freelance Web programmer and writer
Sendmail in Perl Posted: May 3, 2006 6:38 PM
Reply to this message Reply

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
Latest Web Buzz Posts
Latest Web Buzz Posts by Douglas Clifton
Latest Posts From blogZero

Advertisement

cpan People love to argue. A recent thread on the DC Perl Mongers mailing 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()

Read: Sendmail in Perl

Topic: DVCS Mini Roundup Previous Topic   Next Topic Topic: foaf and opml

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use