Gossamer Forum
Home : General : Perl Programming :

Best module for SendMail?

Quote Reply
Best module for SendMail?
Hi

Which one would you say is the best module (I just need a really simple and lightweight one) to handle the *ix Sendmail. Instead of opening a Open(MAIL) ... blah blah each time I'd like to just send $to,$from$subject,$body to a module and away it goes.

Anyone got any reccomendations?

- wil
Quote Reply
Re: [japh] Best module for SendMail? In reply to
You could make you own. Only takes 5 minutes and saves all the opening/closing (and allows you to customize it).

In its simplest form:

Code:
package Mailer;

sub new {
#----------------------------------------------------------
# Create the mailer object.

my ($self,$opts) = @_;

my $defaults = { server => '|/usr/sbin/sendmail -t -i',
to => 'you@there.com',
from => 'me@here.com',
subject => 'Howdy Partner',
message => 'Foo Bar' };

for (keys %$defaults) {
$self->{$_} = defined $opts->{$_} ? $opts->{$_} : $defaults->{$_};
}

return bless {}, $self;

}

sub sendmail {
#----------------------------------------------------------
# Send emails using sendmail.

shift;
my $args = shift;

open MAIL, $args->{server} or die $!;
print MAIL "To: $args->{to}\n";
print MAIL "From: $args->{from}\n";
print MAIL "Subject: $args->{subject}\n\n";
print MAIL "$args->{message}\n";
close MAIL;

return;

}

Then you could use:

my $email = new Mailer;

$email->sendmail;

....to send the default email or pass in a hashref of args like:

$email->sendmail ({ to => 'you@there.com', message => 'Boo' });

Its up to you though I guess. I prefer to write my own but you may wish to download one.

Last edited by:

RedRum: Jan 18, 2002, 7:22 AM
Quote Reply
Re: [RedRum] Best module for SendMail? In reply to
Thanks for the reply. I did exactly that last night - well, very rough. Your version is better. Mine is below. But I just thought maybe there was a better one out there somewhere - but thena gain; if it ain't broke...

Code:
$SEND_MAIL = '/usr/lib/sendmail';

#___________________________________________________________________________

package WS::SendMail;


#######################################################################################
#######################################################################################

sub new {
my $class = shift;
return bless {}, $class;
}

#######################################################################################
#######################################################################################

sub send_mail {

my ($to,$from,$sub,$body) = @_;

open(MAIL,"|$SEND_MAIL -t") or die("Can't open mail pipe: $!");

print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Content-Type: text/plain\n";
print MAIL "Subject: $sub\n\n";

print MAIL "$body\n";

close (MAIL);

return;
}

- wil
Quote Reply
Re: [RedRum] Best module for SendMail? In reply to
I have a couple of bits wrong in sub new() as I just wrote that on the spur of the moment but you get the idea.

Last edited by:

RedRum: Jan 18, 2002, 7:28 AM
Post deleted by RedRum In reply to

Last edited by:

RedRum: Jan 18, 2002, 7:52 AM