How to send Mail to email addresses in mysql table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

How to send Mail to email addresses in mysql table

Post by adsegzy »

Hello Friend,

I tried sending mail to the 30 email addresses in mysql table with the below code. But the problem am having is that, the code is sending the same mail to all the emails 30 times.

Code: Select all


$get_emails = mysql_query("SELECT email FROM customers");
						
while($row = mysql_fetch_array($get_emails)){
	 $email = $row[email];

	mail($email, $sub, nl2br($message), $headers);
}
I also tried something like this but still gave the same result.

Code: Select all


$get_emails = mysql_query("SELECT email FROM customers");
						
$all_emails = array();
while($row = mysql_fetch_array($get_emails)){
	 $all_emails[] = $row[email];
 }
	foreach($all_emails as $to_email){
	mail($to_email, $sub, nl2br($message), $headers);
}
}
Pls what's the way out

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to send Mail to email addresses in mysql table

Post by requinix »

The first one is correct and the second is functionally identical.

If you look at all the "email"s in the "customers" table, what do you see? Does it look right?
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: How to send Mail to email addresses in mysql table

Post by adsegzy »

Thanks Spammer :|,

The emails in customers are ok. Just that it sends to all the 30 emails 30 times. Noe the emails has increased to 34, it will sent the same email to them 34 times.
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

Re: How to send Mail to email addresses in mysql table

Post by adsegzy »

Thanks,
I have sorted it out
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to send Mail to email addresses in mysql table

Post by requinix »

I'm guessing you had another loop, not included in the code you posted, that was running once for each member?
Post Reply