I have created this small script to email rows from mysql database. Code: <?php include_once('include/database.php'); // Database connection details include_once('include/database.class.php'); // access database functions $db=new database(DBHOST,DBNAME,DBUSER,DBPASS); // database connection $sql=mysql_query("SELECT * FROM news ORDER BY newsId LIMIT 0,5") or die (mysql_error()); while($del=mysql_fetch_assoc($sql)){ $id = $del['newsId']; $content = $del['content']; $title = $del['title']; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; ?> <?php mail( "someguy@gmail.com", "$title", $content, "$headers" ); $sql3="DELETE FROM news WHERE newsId='".$del['newsId']."'"; $res=mysql_query($sql3)or die (mysql_error()); } ?> It works great just like i wanted, sends 5 email from table 'news' based on ascending numbers in row 'newsId'. but i would like to make a change now, i would like the script to send one email each to different email id's from the row "email" in the table "emailaddress" randomly. So that in case there is only 3 rows of content in "news" table the email is sent to random 3 email address from the table "email address". I have tried lot of things but i just can't find any working solution.
If I am understanding you correctly, replace the mail with: $getEmail = mysql_fetch_assoc(mysql_query("SELECT `email` FROM `emailaddress` ORDER BY RAND() LIMIT 1")); mail( $getEmail['email'], "$title", $content, "$headers" ); Code (markup):
thnx for your reply but i am afraid this will randomly select one email address and send 5 emails to it, instead of sending 5 different emails to 5 email addresses. Still I'll check it up and post an update.. Thanks again. UPDATE: I was wrong, you are indeed The Php master