chimni has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks. The following is a function i use to send mail. It is quite simple.all i want to do is send an email. No attachments etc. required. Could anyone tell me how to make this sub routine more efficient. Is there any reason for me to use any of the mail modules on CPAN to get just this basic functionality.
would this be better than calling
system("mailx -s ..."); or
`mailx -s ..` from my script

sub sendmail { my($from,$recipients,$list,$sub,$msg,@timedown) = @_; open(MAIL, "|/usr/lib/sendmail -t"); my $old=select(MAIL) ; print<<"EOF" ; To: $recipients CC: $list From:$from Subject: $sub Body: @timedown $msg EOF close(MAIL); select($old); }

Replies are listed 'Best First'.
Re: Sub routine used to send a mail
by barrd (Canon) on Sep 30, 2003 at 09:59 UTC
    I prefer to use MIME::Lite just to make life easier, following is a short sample:
    use MIME::Lite; my $msg = MIME::Lite->new( From => $from_address, To => $to_address, Subject => 'Hello world', Data => 'some body text' ); $msg->send;
    This can of course be incorporated into a sub if required.

    Update: fixed some obvious syntax errors (well, obvious after a cup o' joe ;)

Re: Sub routine used to send a mail
by jdtoronto (Prior) on Sep 30, 2003 at 12:37 UTC
    There must have been thousands of prgrammes written using the 'direct to sendmail' approach. Gee, I must have done it 30 or 40 times myself. If it ain't broke, don't fix it. It is probably the most portable way of sending mail on a *nix machine.

    That being said, I am using MIME::Lite in my programmes now, largely becuase my clients are asking for HTML formatted emails, or they want the prog to mail a pdf doc created dynamically or some such. Plain text just does not cut it for some folk.

    jdtoronto