Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sendmail / PHP ....TIME OUT PROBLEM

Status
Not open for further replies.

pain4u

Technical User
Jun 26, 2001
64
US
aaargh, I'm frustrated with this stuff...I really need someone to help me! (again)

I am not too familiar with configuring Sendmail (and I don't still get what the hell M4 is all about)...but I know what I am doing to an extent with Sendmail. I just need some help with possibly a configuration directive (a time out maybe)...here is my scenario:

I have a list of about 20,000 subscribed emails (I am running FreeBSD). I'm using a PHP script that I've written to query a mySQL database for email address's and then send an email out to each one. But my problem is that I am only able to send 1,000 or so at a time. I believe this is a problem from within Sendmail because I have already setup my Server & PHP to allow no time outs whatsoever! --I've been trying to figure this out for a day or 2 now.

I have done some error handling / debugging and it looks like the connection status is Normal & OK between my script, the server, the client and the database...SO, that means it must be Sendmail timing out. I have tried setting up Multiple Queues, and that did not help any. The reason why I think it is timing out and not hitting a message send limit is because I have had it give me the same error/problem with only 200 - 500 messages being sent from within the same length of time as it took to send 1,000+ (I have tested this!). SENDMAIL MUST BE TIMING OUT!

Can anyone help me??
--------
(even tho I know my PHP script works fine)
HERE IS MY CODE anyways:
--------
<?php
function sendStart()
{// Start & build email_id evaluation integers, variable & array definitions, and the SQL conditional expressions for each query!
$a = 16566;
$b = 500;
$x = 0;
$i = 0;

$evals2 = array(0,1,2,3,4,5,6,7,8,9);
$temp = array();
$temp2 = array();
$evals = array();
$dba = array(&quot;hostname&quot;,&quot;user&quot;,&quot;password&quot;,&quot;DBname&quot;);

$file = &quot;email_body.txt&quot;;
$logfile = &quot;email_send.log&quot;;
$logfile2 = &quot;email_send_skipped.log&quot;;

$fd = fopen($file,&quot;r&quot;);
$body = fread($fd,filesize($file));
fclose($fd);

$fp = fopen($logfile,&quot;w&quot;);
$fp2 = fopen($logfile2,&quot;w&quot;);

foreach($evals2 as $eval)
{if($eval == 0 && $i == 0)
{$eval = $a + $b;
$temp[$i] = $eval;
$y = $i;
}

if($i > 0 && $eval <= count($evals2) - 1)
{$eval = $temp[$y] + $b;
$temp[$i] = $eval;
$y = $i;
}
$evals2[$i] = $eval;
$i++;
}

foreach($evals2 as $eval)
{$eval++;
$temp2[$x] = $eval;
$x++;
}

for($i=0;$i<count($evals2);$i++)
{if(isset($e))
{if($i == count($evals2) - 1)
break;
$e = $i + 1;
$evals[$i] = &quot;> $evals2[$i] AND email_id < $temp2[$e]&quot;;
}
else
{$e = $i + 1;
$evals[$i] = &quot;>$evals2[$i] AND email_id < $temp2[$e]&quot;;
}
}

// Start database queries & email sending for each matching record/row found!

foreach($evals as $eval)
{$link = mysql_connect($dba[0],$dba[1],$dba[2]);
if(!$link)
die(&quot;Couldn't connect to mysqld!&quot;);

mysql_select_db($dba[3],$link) or die(&quot;Couldn't open $dba[3]: &quot;.mysql_error());

if(isset($delay))
sleep(20);
$query1 = &quot;SELECT email,email_id FROM email_list WHERE email_id $eval&quot;;
$result = mysql_query($query1,$link);
if(!$result)
{print mysql_error();
print mysql_affected_rows();
}
while($rows = mysql_fetch_assoc($result))
{if(file_exists($logfile))
$fp = fopen($logfile,&quot;a&quot;);
$send = mail($rows,&quot;EMAIL SUBJECT!&quot;,$body,&quot;From: myemail@mydomain.com (A1SuperCruises.com)\nContent-Type: text/plain\n&quot;);
$last_id = $rows[email_id];
fputs($fp,&quot;$rows[email] - $rows[email_id] - $x\n&quot;);
$x++;
}
flush();
$delay = 1;
mysql_free_result($result);
}
fclose($fp);
fclose($fp2);
}

function shutdown_info()
{$error_log = &quot;email_send_errors.log&quot;;
$fp = fopen($error_log,&quot;w&quot;);
if(connection_aborted())
fputs($fp,&quot;The client has disconnected.<br>\n&quot;);
if(connection_timeout())
fputs($fp,&quot;The connection has timed out.<br>\n&quot;);
if(mysql_error())
fputs($fp,&quot;An error has occurred in mySQL: &quot;.mysql_error().&quot;<br>\n&quot;);
fputs($fp,connection_status());
}
// Do the job right!
ignore_user_abort(true);
set_time_limit(0);
register_shutdown_function(&quot;shutdown_info&quot;);
sendStart();
?> [b]- PAINKILLER [/b]
 
Hi,

Even if you set the correct values for time limit and user-abort, you still need to consider another factor: the default configuration of PHP doesn't allow any one script to use more than 8 MB of RAM. I can't guarantee it, but that sounds like it might be your problem. Look in php.ini:
Code:
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; 
memory_limit = 8M      ;

You have taken care of the first limitation with you script, but in order to make a greater amount of memory available to one script, you need to change the settings in PHP.ini, and restart Apache.

As I said, this is not a guarantee, only an educated guess, since your script above looks pretty memory-intensive.

You might just want to look into a Perl command-line script to handle your mailing list. I'm sure there are example Perl scripts at CPAN or elsewhere that can be easily converted to your needs. Then you could just call that script with an exec() call in PHP.

Also -- some Unix people might burn me at stake for this -- but Sendmail is not exactly the fastest mail server. You might want to check out Qmail, Exim, or Postfix.
 
I have actually already increased the memory_limit to 48 MB. I think that should be more than enough...and the max execution is -1 (no limit), plus I use the set_time_limit() function.

Do you know anything about the session.gc_maxlifetime setting in the php.ini? Does that effect my send? Also, I really believe that it is Sendmail timing out or hitting a limit...NOT php or my server. I have already done alot of testing on this, and I am pretty sure of that...

As for a Perl command-line script, I have considered that..although I can not code Perl too well (I prefer PHP). I know that PHP can do the job...something is messing me up with Sendmail, I think. But...if it leaves me no other choice...then I might have to do that (use Perl) even though I don't really want to. - PAINKILLER
 
Well, I don't really have any gems of wisdom for you here, sorry. I'm not too familiar with Sendmail troubleshooting. You might try posting this question in the Unix or Linux server forums.

Also, as I said, I have heard very good things about Qmail's ability to handle large amounts of email ( I believe there is even a method to allow Qmail to use MySQL as its data storage mechanism. Also, you might be interested in software such as EZMLM ( and which is software explicitly built for processing large email lists. In fact, there is even a PHP interface to EZMLM (
If you are stuck with Sendmail, and can't figure out the problem, your next best bet might be a simple work-around: Have PHP break the job up into smaller chunks, where it processes about 500 emails, disconnects and waits a couple seconds (make sure you unset() all variables to clear memory), and then goes on to process the next group of 500, etc...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top