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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to loop each email address in a for loop 1

Status
Not open for further replies.

pushyr

Programmer
Jul 2, 2007
159
GB
i have problem with a bulk email script. everything works fine by getting the email addresses and sending an html email... every 1 second using sleep, then sleeping for 10 seconds after sending a batch of 30.

now what i want to do is insert its email address as a tracking code into each mail. here's the unmodified code...


Code:
	//query email addresses from db
	
	$SQL = "SELECT * "; 
	$SQL .= "FROM email_list ";
	$SQL .= "WHERE store_code = '$category' ";
	
	if(!$result = mysql_query($SQL)) die("Query died for $SQL");
	
	while($row = mysql_fetch_array($result))
	{
	
	//collect emails in array
	
	$EmailAddress2[] = $row["email"];
		
	}
	
	
	
	$mailbody = stripslashes($content);
	$max= count($EmailAddress2);
	
	$hold=0;
	$letters_sent=0;
	$notsent=0;
	set_time_limit(0);
	for ($i=0; $i < ($max); $i++) {

		if(mail($EmailAddress2[$i], $subject2, $mailbody, $headers)) {
		
			$hold++;
			$letters_sent++;
			echo($EmailAddress2[$i]." <b>sent!</b><br />");
			//there should be a 1 second delay between emails. 
			
			sleep(1);
			if($hold==30) { //if 30 emails sent - code rest for 10 seconds then execute after 
			
				sleep(10);
				$hold=0;
			}
		
		} else {
		
			$notsent++;
			echo($EmailAddress2[$i]." <font color=\"#ff0000\"><b>not sent</b></font><br />");
		
		// end if	
		}
	
	// end for
	}
	$sentstatus="<p>Newsletter sent!</p>";
	?>
	
	<b><?=$letters_sent?></b> newsletters sent
	<br>
	<b><?=$notsent?></b> newsletters failed


when i modify the code by doing this...


Code:
$hold=0;
	$letters_sent=0;
	$notsent=0;
	set_time_limit(0);
	for ($i=0; $i < ($max); $i++) {

$mailbody .= '<img src="image.php?id='.$EmailAddress2[$i].'">';

		if(mail($EmailAddress2[$i], $subject2, $mailbody, $headers)) {
		
			$hold++;
			$letters_sent++;
			echo($EmailAddress2[$i]." <b>sent!</b><br />");
			//there should be a 1 second delay between emails. 
			
			sleep(1);
			if($hold==30) { //if 30 emails sent - code rest for 10 seconds then execute after 
			
				sleep(10);
				$hold=0;
			}
		
		} else {
		
			$notsent++;
			echo($EmailAddress2[$i]." <font color=\"#ff0000\"><b>not sent</b></font><br />");
		
		// end if	
		}
	
	// end for
	}


$mailbody .= '<img src="image.php?id='.$EmailAddress2[$i].'">';

what i get is a loop of all the email addresses in each html email. what am i doing wrong?

 
Code:
$mailbody .= '<img src="image.php?id='.$EmailAddress2[$i].'">';
By using the dot-equals (.=) notation you are appending an img tag to what's already stored in $mailbody. So with each iteration through your for loop, $mailbody grows and what is currently stored in $mailbody is sent in each email!

Instead, why not do something like the following, whereby you don't append to $mailbody, you simply pass $mailbody and the img tag into the mail function:
Code:
...
for ($i=0; $i < ($max); $i++) {
  if(mail($EmailAddress2[$i], $subject2, $mailbody . '<img src="image.php?id='.$EmailAddress2[$i].'">', $headers)) {
...

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top