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

I've been using the following sendmail code (or similar) in many of my scripts:

#begin code# my $mail_prog = "/usr/sbin/sendmail"; my $to = $txtnumber; my $reply_to = $txtnumber; my $from = $txtnumber; open (MAIL, "|$mail_prog -t -n -oi"); print MAIL "To: $to <$to>\n"; print MAIL "Reply-to: $reply_to <$reply_to>\n"; print MAIL "From: $from <$from>\n"; print MAIL "Subject: $subject\n"; print MAIL "\n\n"; print MAIL "$message\n"; close(MAIL); #end code#
It's never been blazing fast, but always got the job done. Just recently however, I noticed things have slowed way down. I have to wait forever just to send one e-mail. If I try to send 3 or 4 e-mails, the browser window will usually time out waiting for the script to finish.

Has anyone experienced anything like this? Can you think of anything that would have caused this sendmail function to slow down so much? When I use Openwebmail on the same server (which also uses sendmail), it sends e-mails very quickly. This leads me to believe that it's not a sendmail problem.

If anyone has any ideas of what's slowing things down, please share them with me. Also, I noticed that many people are using Mail::Send and Mail::Mailer. Do you think these will speed things up? Also, how do Mail::Mailer and Mail::Send compare to each other?

Thanks in advance.

JD
UPDATE: I dug through the maillog file a bit, googled some errors I found, and ended up getting things to work properly. It ended up having nothing to do with Perl. I just had to add a few entries to the /etc/hosts file. Thanks everyone for the help.

Edited by planetscape - added code tags

( keep:0 edit:25 reap:0 )

Replies are listed 'Best First'.
Re: sendmail code running extremely slow
by sgifford (Prior) on Jun 15, 2006 at 01:34 UTC
    That's the pretty standard way to send mail on a Unix system. There are lots of reasons why this could be slow; you probably want to ask the administrators of the system where your script is running why it's so slow, or look at the logs if you have them.

    You could also try the option -oDeliveryMode=b, which asks sendmail to return immediately, even if the message isn't delivered yet.

    You could also try sending the message by SMTP, which isn't any better, but may happen to be faster on this particular system.

      Unfortunately, I am the admin of this server. I can run the mail command from the CLI and it gets sent in a split second. It's just the mail code within the perl script. I tried the -oDeliveryMode=b option, but it didn't seem to make it run faster.
        There should be no real difference between running from the command-line and from a Perl script. If you run your script from the command-line, does the message take a long time to send? What about if you try a second time? What if you switch to the Web server user before doing so? Do you do anything "weird" in your Web server setup, like chroot? What shows up in your Web server log? In your mail server log? Do you see the exact same effect if you run the small test script you included in your OP, both running from the command-line and the Web server?
Re: sendmail code running extremely slow
by graff (Chancellor) on Jun 15, 2006 at 04:07 UTC
    I looked up the man page for sendmail on my bsd-based mac, to check the options you use, and I was struck by the description of "-t": "Extract recipients from message headers. These are added to any recipients specified on the command line."

    I have no idea whether that would have any impact (it probably wouldn't), and to check, you would probably want to switch to a multi-arg open, to make sure that email addresses wouldn't screw up the command line:

    open( MAIL, "|-", $mailprog, $to ); # ... or die?
    (My man page also indicated that "-n" is ignored (backwards compatability).

    I have used Mail::Send to good effect -- that might be worth a try as well, esp. since it's usage is not very different from what you are doing already.

Re: sendmail code running extremely slow
by graff (Chancellor) on Jun 15, 2006 at 04:09 UTC
    I looked up the man page for sendmail on my bsd-based mac, to check the options you use, and I was struck by the description of "-t": "Extract recipients from message headers. These are added to any recipients specified on the command line."

    I have no idea whether that would have any impact (it probably wouldn't), and to check, you would probably want to switch to a multi-arg open, to make sure that email addresses wouldn't screw up the command line:

    open( MAIL, "|-", $mailprog, "-oi", $to ); # ... or die?
    (My man page also indicated that "-n" is ignored (backwards compatability).

    I have used Mail::Send to good effect -- that might be worth a try as well, esp. since it's usage is not very different from what you are doing already.