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);
}
> sub sendEmail
> {
> my ($to, $from, $bcc, $subj, $message) = @_;
> my $sendmail = '/usr/lib/sendmail';
> my $line;
You should declare variables in the smallest possible scope.
For this variable, it should be declared in the foreach that
uses it as the loop control variable (below).
> my @rawsender = $from;
> open(MAIL, "|$sendmail -oi -t -f $rawsender[0]");
Why do you copy it to an array?
You should always, yes *always*, check the return value from open().
See also:
perldoc -q "pipe open"
> $message =~ s/\r/\n/g;
faster and more clear:
$message =~ tr/\r/\n/;
> my @lines = split("\n", $message);
Here you remove all of the newlines...
> 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){
foreach my $line (@lines){
> print MAIL "$line \n";
... here you put all of the newlines back.
What is the point of removing newlines only to put them back?
Do you really require a space character at the end of each line?
I don't see where you output a blank line to mark the end of the headers...
> close(MAIL);
You should check the return value from close() here too.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
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]
--
[pl>en Andrew] Andrzej Adam Filip : an...@onet.eu : Andrze...@gmail.com
"There is no statute of limitations on stupidity."
-- Randomly produced by a computer program called Markov3.
I've never used it myself, but you might want to look at using
Mail::Bulkmail
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
If your perl supports threads then redirect your question to
news:comp.mail.sendmail 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" ].
--
[pl>en Andrew] Andrzej Adam Filip : an...@onet.eu : Andrze...@gmail.com
"Why are we importing all these highbrow plays like `Amadeus'? I could
have told you Mozart was a jerk for nothing."
-- Ian Shoales
[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)
Jamie
--
http://www.geniegate.com Custom web programming
Perl * Java * UNIX User Management Solutions
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