Programming :  Student Freelance Forum For Work Experience Builders' (CertificationPoint) The fastest message board... ever.
 
PERL-Sending Email from mod_perl
Posted by: adcertpoint (Moderator)
Date: April 29, 2020 04:39PM

There is nothing special about sending email from mod_perl, it's just that we do it a lot. There are a few important issues. The most widely used approach is starting a sendmail process and piping the headers and the body to it. The problem is that sendmail is a very heavy process and it makes mod_perl processes less efficient.

If you don't want your process to wait until delivery is complete, you can tell sendmail not to deliver the email straight away, but either do it in the background or just queue the job until the next queue run. This can significantly reduce the delay for the mod_perl process which would otherwise have to wait for the sendmail process to complete. This can be specified for all deliveries in sendmail.cf or on each invocation on the sendmail command line:

•-odb (deliver in the background)


•-odq (queue-only) or


•-odd (queue, and also defer the DNS/NIS lookups).


The trend is to move away from sendmail(1) and switch to using lighter mail delivery programs like qmail(1) or postfix(1). You should check the manpage of your favorite mailer application for equivalent configuration presented for sendmail(1).

The most efficient approach is to talk directly to the SMTP server. Luckily Net:confused smileyMTP modules makes this very easy. The only problem is when Net:confused smileyMTP fails to deliver the mail, because the destination peer server is temporarily down. But from the other side Net:confused smileyMTP allows you to send email much faster, since you don't have to invoke a dedicated process. Here is an example of a subroutine that sends email.
use Net:confused smileyMTP ();
use Carp qw(carp verbose);

#
# Sends email by using the SMTP Server
#
# The SMTP server as defined in Net::Config
# Alternatively you can hardcode it here, look for $smtp_server below
#
sub send_mail{
my ($from, $to, $subject, $body) = @_;

carp "From missing" unless defined $from ; # Prefer to exit early if errors
carp "To missing" unless defined $to ;

my $mail_message = <<__END_OF_MAIL__;
To: $to
From: $from
Subject: $subject

$body

__END_OF_MAIL__

# Set this parameter if you don't have a valid Net/Config.pm
# entry for SMTP host and uncomment it in the Net:confused smileyMTP->new
# call
# my $smtp_server = 'localhost';

# init the server
my $smtp = Net:confused smileyMTP->new(
# $smtp_server,
Timeout => 60,
Debug => 0,
);

$smtp->mail($from) or carp ("Failed to specify a sender [$from]\n"winking smiley;
$smtp->to($to) or carp ("Failed to specify a recipient [$to]\n"winking smiley;
$smtp->data([$mail_message]) or carp ("Failed to send a message\n"winking smiley;

$smtp->quit or carp ("Failed to quit\n"winking smiley;

} # end of sub send_mail

Options: ReplyQuote


Sorry, only registered users may post in this forum.
This forum powered by Phorum.