DW,
THanks for the reply, can you please give me a little info on how the 'queue' works?
I cannot wait any longer for your update and must get this handled ASAP, I'm going to have to write my own background mail handler that jump starts the domail.php queue in PERL via a CRON job.
If you can give me a little insight into it, it will be very helpful...
I see this code in admin.php - the send mail queue loop - this is what I will use to create another module that will handle jump starting the queue by running from CRON every 15mins or so? I also see that records are created in lm_sendq (the queue table) - I see those records are deleted one as a time as they are sent - so my guess is I only need to check mrows (number of rows) in lm_sendq and if there are any I can jump start those - IF that number stays the same the next time I run my CRON job - in other words if I wait 15 minutes (and I have saved num_rows) in lm_sendq and it's the same when CRON runs again, I should assume that the queue is stuck and start it up again - how's that sound?
Does that sound right, and any insight you can give on other code I should look at would be helpful... I really don't have any other alternative as I have to get this working - I can't sit here and press RESUME all day long with large sendings...
Thanks,
-Brett
// queue loop !!!
// read from lm_sendq and process..
$cmd = "select id,mtype,uid,mid,xtra from $otable where bat = '$batch'";
$mrows = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
// display send box
$nmails = @mysql_num_rows($mrows);
$txsize = strlen($nmails);
if($dayo || $solo) {
echo "<center></span><span id=delaymsg style=\"font: italic 8pt helvetica; display: none\">Delaying.. Please wait..</span><br><span class=table_inside_normal>If your mailing stops for any reason, close the window or <input class=button type=button onclick=\"hopto('./domail.php?resume=1&batid=$batch&list=$list')\" name=bs value=\"click here\">. Do NOT Refresh!<br><br>Sending Mail <input type=text name=disp size=$txsize value=0> of $nmails..</span><br>";
flush();
}
if(!$mailcount) $mailcount = 50;
if(@mysql_num_rows($mrows)>0){
while(list($oid,$mtype,$uid,$msgid,$xtra)=mysql_fetch_row($mrows)){
$skiptonext = '';
$error = '';
if($mtype<>'5'){
// get user send vars
$cmd = "select uid,list,fname,lname,email,htmail,bounces from $utable where id = '$uid'";
$urow = mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($unid,$mlist,$fname,$lname,$email,$htmail,$bounces)=mysql_fetch_row($urow);
if(strpos($email,'@aol.com')>0 && !$html_to_aol) $aol = 1; else $aol = '';
// echo "UID=$uid UNID=$unid EMAIL=$email<br>";
}
// get message
if($mtype=='1' && (!$solo || $xid == 'resume')){
$cmd = "select subject,content,htcontent,fattach from $ttable where id = '$msgid'";
$msgrow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($subj,$msg,$htmsg,$fatt) = @mysql_fetch_row($msgrow);
}
if($mtype=='2'){
// followup
$cmd = "select subject,content,htcontent,fattach from $ftable where id = '$msgid'";
$msgrow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($subj,$msg,$htmsg,$fatt) = @mysql_fetch_row($msgrow);
}
if($mtype=='3'){
// welcome message
$cmd = "select welsubj,welcom,welcht,welcf from $ltable where list = '$mlist'";
$msgrow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($subj,$msg,$htmsg,$fatt) = @mysql_fetch_row($msgrow);
}
if($mtype=='4'){
// confirm message
$cmd = "select cnfsubj,cnfmesg,cnfht,cnff from $ltable where list = '$mlist'";
$msgrow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($subj,$msg,$htmsg,$fatt) = @mysql_fetch_row($msgrow);
}
if($mtype=='5'){
$cmd = "select list from $utable where id = '$uid'";
$urow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($mlist)=@mysql_fetch_row($urow);
// admin email.. msgtype in sendq $xtra
$msgtype = $xtra;
if($msgtype=='bounce' || $msgtype=='daily'){
$xmails = explode(';',$admmail);
$admmail = $xmails[0];
$email = $admmail;
// saved table
$cmd = "select subject,content,htcontent,fattach from $stable where id = '$msgid'";
$msgrow = @mysql_query($cmd) or die("mysql select prob");
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($subj,$msg,$htmsg,$fatt) = @mysql_fetch_row($msgrow);
$cmd = "delete from $stable where id = '$msgid'";
@mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
}
$today = date("D M jS H:m T");
if($msgtype=='daily'){ $subj = "Dailymail - $today"; }
// get ip and ref url
global $REMOTE_ADDR;
global $HTTP_REFERER;
$ipaddr = $_SERVER['REMOTE_ADDR']; if(!$ipaddr) $ipaddr = $REMOTE_ADDR; if(!$ipaddr) $ipaddr = "Unknown";
$refurl = $_SERVER['HTTP_REFERER']; if(!$refurl) $refurl = $HTTP_REFERER; if(!$refurl) $refurl = "Unknown";
$iphost = @gethostbyaddr($ipaddr);
if(!$sendn) $sendn = 'ListMail Notification';
$sende = $errmail;
} else {
// not admin msg, continue
$cmd = "select sendn,sende from $ltable where listnum = '$mlist'";
$lrow = @mysql_query($cmd);
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
list($sendn,$sende)=@mysql_fetch_row($lrow);
// echo "got list settings<br>"; flush();
}
$y++;
// send the message
if($subj && $msg){
// echo "processing message...<br>"; flush();
// process message codes
if($mtype<>'5'){
$xsubj = processmsg($uid,$subj,'0',$mtype,$msgid);
$xmsg = processmsg($uid,$msg,'0',$mtype,$msgid);
if($aol){
// $xmsg = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i","<a href=\"$1\">$1</a>",$xmsg);
// $xmsg = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i","<a href=\"mailto:$1\">$1</a>",$xmsg);
}
$xhtmsg = processmsg($uid,$htmsg,'1',$mtype,$msgid);
} else {
// echo "SUBJ=$subj,MSG=$msg.<br>";
$xsubj = $subj; $xmsg = $msg; $xhtmsg = $htmsg;
}
// init MIME message
$mail = new html_mime_mail();
// html message
if($htmsg && $htmail == '1' && !$aol) $mail->add_html($xhtmsg, $xmsg, '');
// text message
if(($msg && !$htmsg) || ($msg && $htmsg && ($htmail == '0' || $aol))) $mail->add_text($xmsg);
// add file attachments
if($fatt) {
$xfatt = explode(',',$fatt);
while(list($key2,$filen) = each($xfatt)){
setfiletype($filen); // set atype and etype
global $atype;
global $etype;
if(!$atype) $atype = 'application/binary';
// check file exists before adding
$result = file_exists("./attach/" . $filen);
if($result==true){
$attachment = $mail->get_file("./attach/$filen");
$mail->add_attachment($attachment, $filen, $atype, $etype);
}
}
}
// build final message
// default charset for MIME message, improve this.
$dcharset = "iso-8859-1";
if($charset) $bchar = $charset; else $bchar = $dcharset;
$build_params = array();
$build_params['html_charset'] = $bchar;
$build_params['text_charset'] = $bchar;
// $build_params['text_wrap'] = '30';
$mail->build_message($build_params);
// testing!!
/* $body = $mail->output;
echo "<br><Br>".nl2br($body); */
// build full name for to: field
if($mtype=='5') $fulln = 'ListMail Admin';
else {
$fulln = $fname;
if($lname && $lname<>'0') $fulln .= ' ' . $lname;
}
// final message
$index = 0;
// $bad_rcpt = array();
// $smtp_from = $errmail;
// $smtp->Sender = $errmail;
// calculate date/time
$tz = date("Z");
$tzs = ($tz < 0) ? "-" : "+";
$tz = abs($tz);
$tz = ($tz/3600)*100 + ($tz%3600)/60;
$date = sprintf("%s %s%04d", date("D, j M Y H:i:s"), $tzs, $tz);
$body = $mail->output;
$headers = $mail->headers;
$header = '';
$crlf = "\n";
if($smtpsend) $header .="Date: $date$crlf";
$header .="From: \"$sendn\" <$sende>$crlf";
if($smtpsend)
if($fulln) $header .= "To: \"$fulln\" <$email>$crlf";
else $header .= "To: $email$crlf";
if($smtpsend) $header .="Subject: $xsubj$crlf";
if(!$smtpsend) $header .= "Return-Path: $errmail$crlf";
$header .= "MIME-Version: 1.0$crlf";
while(list($key2,$data) = each($headers)){
// $data1 = str_replace("\r","\\r",str_replace("\n","\\n\n",$data));
// echo "data1=<b>$data1</b><br>";
$header .= $data.$crlf;
}
$header .="X-Mailer: ListMail v$ver";
if($smtpsend) $header .= "$crlf";
if(($htmsg && $htmail && !$aol) || $fatt){
$header .= $crlf."This is a multipart message in MIME format.";
if($smtpsend) $header .= $crlf.$crlf;
} else
if($smtpsend) $header .= "$crlf";
// final send
// echo "going to final send<br>"; flush();
if($smtpsend){
// echo "smtpsend..<br>"; flush();
if($demo <> 'yes') {
// need to POP check?
if($mtpchk){
$now = date("YmdHis");
$diff = $now - $starttime;
if($diff > $mtpdelay){
$asock = fsockopen($mtphost, $mtpport);
if(!$asock) $error = 1; else $error = '';
if(!$error){
// connected!
$srvmsg = fgets($asock, 1024);
$lastmsg = substr($srvmsg, 0, 1);
if ($lastmsg <> "+") $error = 1; else $error = '';
if(!$error){
// send user
fputs($asock, "USER $mtpuser\r\n");
$srvmsg = fgets($asock, 1024);
$lastmsg = substr($srvmsg, 0, 1);
if ($lastmsg <> "+") $error = 1; else $error = '';
// send pass
fputs($asock, "PASS $mtppass\r\n");
$srvmsg = fgets($asock, 1024);
$lastmsg = substr($srvmsg, 0, 1);
if ($lastmsg <> "+")$error = 1; else $error = '';
// if($error) echo " Failed!<br>"; else echo " Verified!<br>";
if(!$error){
// check # of messages
fputs($asock, "STAT\r\n");
$srvmsg = fgets($asock, 1024);
$lastmsg = substr($srvmsg, 0, 1);
if ($lastmsg <> "+") $error = 1;
}
// QUIT
fputs($asock, "QUIT\r\n");
$srvmsg = fgets($asock, 1024);
$lastmsg = substr($srvmsg, 0, 1);
if ($lastmsg <> "+") $error = 1;
sleep(4);
}
} // else failed connect
$starttime = $now;
}
}
// echo "check smtp delay..<br>"; flush();
// check smtp delay
$mtimer++;
if($mtimer > $mtdelay) {
fputs($ssock, "QUIT\r\n");
$srvmsg = @fgets($ssock, 1024);
$reconnect = 1;
$skiptonext = '';
$mtimer = 1;
}
if(!$ssock) $reconnect = 1;
// echo "check reconnect.. value=$reconnect<br>"; flush();
if($reconnect || !is_resource($ssock)){
if($bugs){ echo "reconnecting!"; flush(); }
// (re)connect to smtp
$ssock = fsockopen($mthost, $mtport) or die('could not open socket with fsockopen()');
if(!$ssock) $error = 1; else $error = '';
if(!$error){
// connected!
$srvmsg ='';
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "CONNECTEDmsg=".nl2br($srvmsg); flush(); }
if(!$error){
if(!$error){
$hhost = @getenv('HTTP_HOST');
if(!$hhost) $hhost = 'listmail';
else {
// echo substr($hhost,0,4);
if(substr($hhost,0,4)=='www.')
$hhost = substr($hhost,4,strlen($hhost)-1);
}
// send ehlo
fputs($ssock, "EHLO localhost\r\n");
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "EHLOmsg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250"){
if($lastmsg=="500"){
if(!$helohost) $helohost = 'localhost';
fputs($ssock, "HELO $helohost\r\n");
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "HELOmsg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if($lastmsg <> "250") $error = 1; else $error = '';
} else $error = 1;
} else $error = '';
}
// check pipelining, need to finish this
if(strpos($srvmsg,'PIPELINING')>0) $pipeline = 1; else $pipeline = '';
// $pipeline = ''; echo "PIPELINING OFF!<br>"; flush();
// auth
if($mtauth && strpos($srvmsg,'AUTH LOGIN')>0){
// EHLO says auth is good
fputs($ssock, "AUTH LOGIN\r\n");
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "AUTHmsg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "334") $error = 1; else $error = '';
if(!$error){
// send username
fputs($ssock, base64_encode($mtuser)."\r\n");
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "AUTHUSERmsg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "334") $error = 1; else $error = '';
if(!$error){
// send password
fputs($ssock, base64_encode($mtpass)."\r\n");
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "AUTHPASSmsg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "235") $error = 1; else $error = '';
}
if($error) echo "Bad password<br>";
} else {
echo "Username not accepted.<br>";
}
$noplain = 1;
} else {
// echo "AUTH command not accepted.<br>";
}
// auth plain
if($mtauth && strpos($srvmsg,'AUTH PLAIN')>0 && !$noplain){
// EHLO says auth is good
fputs($ssock, "AUTH PLAIN ".base64_encode("\0".$mtuser."\0".$mtpass)."\r\n");
// echo "XXXX=AUTH PLAIN ".base64_encode("\0".$mtuser."\0".$mtpass)."\r\n<br>";
$srvmsg = getsmtpmsg($ssock);
if($bugs){ echo "AUTH(plain)msg=".nl2br($srvmsg); flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "235") $error = 1; else $error = '';
if($error){ if($solo||$dayo) echo "User/Pass not accepted.<br>"; }
}
} else {
echo "EHLO failed! ($srvmsg)<br>";
}
} else {
echo "Connect to server <b>$mthost</b>:<b>$mtport</b> failed.<br>";
}
$reconnect = 0;
} // end !connected
// send the mail !
// echo "send the mail!<br>"; flush();
// from
if(!$pipeline){
fputs($ssock, "MAIL FROM: <$errmail>\r\n");
$srvmsg = fgets($ssock, 1024);
if($bugs){ echo "MAILFROMmsg=$srvmsg.<br>"; flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250") $error = 1; else $error = '';
if(!$error){
// check recipient
// echo "EMAIL=$email..<br>";
fputs($ssock, "RCPT TO: <$email>\r\n");
$srvmsg = getsmtpmsg($ssock, 1024);
if($email=='') $srvmsg = '';
if($bugs){ echo "RCPT TO: <$email> RCPTmsg=".nl2br($srvmsg); flush(); }
// if($email == 'badtest@email.com'){ echo " WAS: $srvmsg<br>"; $srvmsg = '550 BADTEST'; }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250") $error = 1; else $error = '';
}
if($error){
// reset
fputs($ssock, "RSET\r\n");
$srvmsg = getsmtpmsg($ssock, 1024);
if($email=='') $srvmsg = '';
if($bugs) echo "RSETmsg=$srvmsg.<br>";
$error = 1;
if($srvmsg<>'') $skiptonext = 1; else $srvmsg = "(Server is not responding to requests after sending ".($y-1)." messages.)<br>";
if($bugs){ echo "Bad RCPT response ($email), skipping.<br><br>"; flush(); }
}
if(!$error){
// start data
fputs($ssock, "DATA\r\n");
$srvmsg = fgets($ssock, 1024);
if($bugs){ echo "DATAmsg=$srvmsg.<br>"; flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "354") $error = 1; else $error = '';
}
} else {
// SMTP pipelining
/* echo "fputs($ssock, \"MAIL FROM: <$errmail>\r\n\");";
echo "fputs($ssock, \"RCPT TO: <$email>\r\n\");";
echo "fputs($ssock, \"DATA\r\n\");"; */
fputs($ssock, "MAIL FROM: <$errmail>\r\n");
fputs($ssock, "RCPT TO: <$email>\r\n");
$srvmsg = fgets($ssock,1024);
if($bugs){ echo "PIPELINE-FROMmsg=$srvmsg.<br>"; flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250") $error = 1; else $error = '';
if(!$error){
$srvmsg = getsmtpmsg($ssock,1024);
if($bugs){ echo "RCPT TO: <$email><br>RCPTmsg=".nl2br($srvmsg); flush(); }
// DEBUG
if($email == 'bad@listmailpro.com'){ echo " WAS: $srvmsg<br>"; $srvmsg = '550 BADTEST'; }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250") {
$error = 1;
} else $error = '';
if($lastmsg == '550'){
fputs($ssock, "RSET\r\n");
$srvmsg = fgets($ssock,1024);
if($bugs){ echo "RSETmsg=$srvmsg<br>"; flush(); }
$error = 1; $skiptonext = 1;
if($bugs){ echo "Bad RCPT response ($email), skipping.<br><br>"; flush(); }
} else $error = '';
if(!$error){
fputs($ssock, "DATA\r\n");
$srvmsg = fgets($ssock,1024);
if($bugs){ echo "DATAmsg=$srvmsg.<br>"; flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "354") $error = 1; else $error = '';
}
}
}
// data - both send methods
if(!$error){
// start data
$themesg = $header.$body;
$themesg = str_replace("\r\n","\n",$themesg);
$themesg = str_replace("\n","\r\n",$themesg);
// echo '['.htmlspecialchars('.').']';
// $themesg = str_replace("=\r\n.",".=\r\n",$themesg);
// $themesg = str_replace("=0D\r\n.",".=0D\r\n",$themesg);
$themesg = str_replace("\r\n.","\r\n..",$themesg);
$xmsg = str_replace('<','<',$themesg);
$xmsg = str_replace('>','>',$xmsg);
// echo nl2br($xmsg);
fputs($ssock, $themesg."\r\n.\r\n");
$srvmsg = fgets($ssock, 1024);
if($bugs){ echo "SENDmsg=$srvmsg.<br><br>"; flush(); }
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "250") $error = 1; else $error = '';
}
}
// BAS MOD - 05/06/04 - THROTTLE CODE, pause 25 seconds every 100 Emails
// 05/07/04 - found out that this code did not increase click throughs
// maybe have to extend the throttle delay longer, or the emails
// are getting sent out, and just blocked or no interest from users
// 07/08/04 - CHECK THAT, it is working, search for Throttle on the forums at
// LIST MAIL PRO for more information
if(($y % '100')==0 && $y <> sizeof($sendq)) sleep(25);
// BAS MOD END
} else {
// use php mail()
$body = str_replace("\r\n","\n",$body);
if($fulln) $email = "\"$fulln\" <$email>";
if ($demo <> 'yes'){
if($phpmailf) mail($email,$xsubj,$body,$header,"-f$errmail");
else mail($email,$xsubj,$body,$header);
}
// if($mailed) echo "mailed ok.<br>"; else echo "mailed failed.<Br>";
// delay
sleep(($del1)*(.001));
if(($y % $del3)==0 && $y <> sizeof($sendq) && $mtype <> '3') {
echo "<script>delaymsg.style.display=\"\"</script>";
flush();
sleep(($del2)*60);
echo "<script>delaymsg.style.display=\"none\"</script>";
}
}
} else {
// !message
echo " Message has no body / subject!<br>";
}
// echo "next??!<br>";
if($error && !$skiptonext){
// echo "sending error!! ";
echo "<span class=status_text style=\"color: red\"><br>Sending error. Check your mail settings.<br><br></span><span class=status_text>Server said: ".nl2br($srvmsg)."<br></span><input class=button type=button onclick=\"hopto('./editconf.php?list=$list')\" name=tc value=\"Go To Config\"><br>";
exit;
}
$cmd = "delete from $otable where id = '$oid'";
mysql_query($cmd) or die('problem with DELETE otable query.<br>');
// optimize table every 1000
if($y % 1000 == 0 || $y == $nmails) mysql_query("optimize table $otable");
// increase display count
if($dayo || $solo){
// echo "checking display count .. y=$y .. mailcount=$mailcount<br>";
// echo "y=$y mailcount=$mailcount DIV=" . ($y % $mailcount)."<br>";
if($sqldebug) echo "CMD=<b>$cmd</b><br>";
if($y % $mailcount == 0 || $y == $nmails) echo "<script>this.disp.value='$y'</script>\n";
// echo "end if<br>";
}
// echo "no sending error! end loop! next!<br>";
flush();
}
} else {
echo "Called domail with no messages in queue, aborting.<br>";
}
if(!$error && $smtpsend){
// start data
fputs($ssock, "QUIT\r\n");
$srvmsg = fgets($ssock, 1024);
if($bugs) echo "QUITmsg=$srvmsg.<br>";
$lastmsg = substr($srvmsg, 0, 3);
if ($lastmsg <> "221") $error = 1; else $error = '';
}
// if(is_resource($ssock)) echo "<b>(Q) server is connected</b><br>"; else echo "<b>(Q) server is not connected</b>";
if($smtpsend) mysql_query("optimize table $qtable");
return true;
} // end domail