perl and sendmail speed problem

J

joe

Hi I wrote a function that uses
sendmail -oi -t -f
to send emails. It is currently taking a long time to send many
messages. Is there a way to speed this up?. Here is the code of the
function

sub sendEmail
{
my ($to, $from, $bcc, $subj, $message) = @_;
my $sendmail = '/usr/lib/sendmail';
my $line;
my @rawsender = $from;
open(MAIL, "|$sendmail -oi -t -f $rawsender[0]");

print MAIL "From: $from\n";

print MAIL "To: $to\n";
if (length($bcc) > 0){
print MAIL "Bcc: $bcc \n";
}
$message =~ s/\r/\n/g;


$message =~ s/!\*EMAIL\*!/$to/g;

$message =~ s/\r/\n/g;


my @lines = split("\n", $message);
print MAIL "Subject: $subj\n";
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-Type: text/html;\n";
print MAIL " charset=\"windows-1252\"\n";

foreach $line (@lines){
print MAIL "$line \n";
}
close(MAIL);
}
 
A

Andrzej Adam Filip

joe said:
Hi I wrote a function that uses
sendmail -oi -t -f
to send emails. It is currently taking a long time to send many
messages. Is there a way to speed this up?. Here is the code of the
function

sub sendEmail
{
[...]
open(MAIL, "|$sendmail -oi -t -f $rawsender[0]");
[...]
}

Have you considered using Net::SMTP to send many messages over single
SMTP session to 127.0.0.1?
[you may use direct execution of sendmail as "fallback" delivery method]

*Be warned*: sending a few hundredth+ messages "may create noticeable load".
[sendmail daemon by default forks separate process per single message delivery]
 
J

joe

Thanks for the tips. Is it possible to run the sub in a separate
thread and have multiple emails sent at the same time? If so how can I
do this? I am sending a few thousand messages so I am concern about
send mail not being able to handle this too.
 
J

joe

I tested this script with only 10 messages and it worked but I am not
sure if it can handle a few thousands

use Thread qw:)DEFAULT async yield);

$thread = Thread->new(\&sendEmail($_,$in_sender,'',$in_subject,
$in_letter));
 
M

Martijn Lievaart

I tested this script with only 10 messages and it worked but I am not
sure if it can handle a few thousands

use Thread qw:)DEFAULT async yield);

$thread = Thread->new(\&sendEmail($_,$in_sender,'',$in_subject,
$in_letter));

Best to limit it to some number of threads so you don't overload your
machine. Wich probably destroys the effect your after.

Do you really need to send 1000 times one message? If the message doesn't
change, send it to many people at once.

If you do need to send individual messages, then it is going to take
time. You may want to investigate why sendmail is so slow.

As an alternative, write the messages to files and start a background
process that sends the messages.

M4
 
J

joe

I think it has to be done individually. It is a email blast script for
our subscribers. I switched to net::smtp and I dont know if it will be
fast enough. If it is too slow I will try use a thread for every other
message.
 
A

Andrzej Adam Filip

joe said:
I think it has to be done individually. It is a email blast script for
our subscribers. I switched to net::smtp and I dont know if it will be
fast enough. If it is too slow I will try use a thread for every other
message.

If your perl supports threads then redirect your question to
to get "hints" how to configure sendmail
for "sky is the limit" performance :)

In short: how to reconfigure sendmail to allow your script control
number of sendmail processes attempting "at once" deliveries
[ one sendmail process per one "perl thread"/"smtp session" ].
 
P

Peter J. Holzer

In said:
Hi I wrote a function that uses
sendmail -oi -t -f
to send emails. It is currently taking a long time to send many
messages. Is there a way to speed this up?. Here is the code of the
function

[snip]

You could optimize the code quite a bit, but I have a feeling it wouldn't
give you the performance you seek.

Your bottleneck is sendmail, even if you split the list up and ran a dozen
of them in parallel, it would only fill up sendmails que. (Giving it the
appearance that you've finished sending)

Sendmail is quite capable of sending huge amounts of mail if you do it
right. However, configuring sendmail is off-topic in this group.

One simple trick (which is marginally on-topic, because it can be done
from the perl script, while configuring queues etc. is the postmaster's
job) which often makes a large difference is to set DeliveryMode=q or
even DeliveryMode=d. Then the sendmail command will just put all the
mails into the queue and leave delivery to the daemon. The daemon can
then figure out the best way to deliver them.

hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top