Click to See Complete Forum and Search --> : looping an sending multiple emails at once


riskmod
04-15-2008, 04:59 PM
I have this :


<?php

$msa = mysql_connect('server','id','pw') or die(mysql_error());
@mysql_select_db('myDB');
$getEmails = "SELECT store_id, id2 FROM stores";
$ansEmails = mysql_query($getEmails) or die(mysql_error());

$emailToString = '';
while($data = mysql_fetch_assoc($ansEmails)){
$emailToString .= " ".$data['store_id']."";
}
$getArticle = "SELECT * FROM stores";
$ansArticle = mysql_query($getArticle) or die(mysql_error());
$article = mysql_fetch_assoc($ansArticle);

mysql_free_result($ansEmails);
mysql_free_result($ansArticle);



$to = "youemail";
$subj = "Test mail";
$from = "From: myemail";
$headers = "From: $from\r\n";


$emailBodyString = " Test " . $article['some data colum from stores'] . " this stuff";


ini_set('SMTP', 'relay');
ini_set("smtp_port", 25);


mail($to, $subj, $emailBodyString, $from, $headers);

?>



Everything works but.....


I need to send this email to a bunch of our clients:

the easy thing is the data that I'm grabbing from the sql has the store_id that matches the persons email. So:

store_id: 1
email: gc1@domain.com

store_id: 2
email: gc2@domain.com

and etc.

I know to get the email and its right data itwould be like this(not sure if right):


$eMails = Array('gc'.store_id.'\@domain.com';);

for ($x = 0; $x < count($eMails); $x++) {

// code here

}



and to loop something like this:



for ($x = 1; $x < 400; $x++) {

$to = 'gc'.$x.'\@domain.com';

}

If so, I just need to know where and how you place these on the code I have.

If this work I might not even need the $emailToString that I started...

Thanks!

SyCo
04-15-2008, 05:32 PM
I'm not sure what you mean exactly. If you have a common ID do you want the id of every store and a corresponding email from another table?

You're only selecting $getEmails = "SELECT store_id, id2 FROM stores";

And hardcoding the $to
$to = "youemail";

Why not just select the email from the database and put the mail() in a loop? If they're in different tables you can join them together using the common id.



PS You'd better not be a spammer!!

riskmod
04-15-2008, 07:07 PM
Ha, no this is only for my internal company (hence the relay port)

And yes for all ids I have to grab each data base on the store_id.

thx