funzmu Posted May 25, 2008 Share Posted May 25, 2008 //Showing all emails in database <?php mysql_connect("localhost","sa","pass"); mysql_select_db("DB"); $qr = mysql_query("select mail_addr FROM Memb_Info"); $nrows = mysql_num_rows($qr); for ($i=0; $i < $nrows; $i++) { $row = mysql_fetch_array($qr); echo $row['mail_addr']."<br><br>"; } //Sending email $to = $row['mail_addr']; $subject = "hi"; $body = "message"; $headers = "From: me@hotmail.com\r\n" . "X-Mailer: php"; if (mail($to, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } ?> When I use this script, it only sends a email to the last person in the database. who can help me? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 25, 2008 Share Posted May 25, 2008 <?php mysql_connect("localhost","sa","pass"); mysql_select_db("DB"); $qr = mysql_query("select mail_addr FROM Memb_Info"); while ($row = mysql_fetch_array($qr)) { $tolist .= $row['mail_addr'] . ", "; } //Sending email $subject = "hi"; $body = "message"; $headers = "From: me@hotmail.com\r\n" . "X-Mailer: php"; if (mail($tolist, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } ?> Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 26, 2008 Author Share Posted May 26, 2008 Allright this works thanks man. Problem now is I use it for 100.000 mailadress and that does not work 100% proper ^^.. I need some time delay in it? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 26, 2008 Share Posted May 26, 2008 You could break it down into chunks. Put the mail function inside a loop cycling through each email address in the $tolist array (or 5 at a time, or 10, or whatever). Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 26, 2008 Author Share Posted May 26, 2008 Sorry, I don't understand, I am just a beginning n00b. I try this: <?php mysql_connect("localhost","sa","pass"); mysql_select_db("DB"); $qr = mysql_query("select mail_addr FROM Memb_Info"); while ($row = mysql_fetch_array($qr) ) { for ($row['mail_addr'] = 1 + mail_addr) $tolist .= $row['mail_addr'] . ", "; } //Sending email $subject = "hi"; $body = "message"; $headers = "From: me@hotmail.com\r\n" . "X-Mailer: php"; if (mail($tolist, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted May 26, 2008 Share Posted May 26, 2008 right...as mentioned, if you want to break it down, put it inside a loop. There are many ways to do this, with different things to consider (lookup php bulk emailing), but one example: <?php mysql_connect("localhost","sa","pass"); mysql_select_db("DB"); $qr = mysql_query("select mail_addr FROM Memb_Info"); while ($row = mysql_fetch_array($qr) ) { for ($row['mail_addr'] = 1 + mail_addr) $tolist .= $row['mail_addr'] . ", "; } //Sending email $subject = "hi"; $body = "message"; $headers = "From: me@hotmail.com\r\n" . "X-Mailer: php"; foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time if ($currentemail, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } } // end foreach ?> Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 26, 2008 Author Share Posted May 26, 2008 Oke, thanks for your time. Helped me alot Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 26, 2008 Author Share Posted May 26, 2008 <?php mysql_connect("localhost","sa","pwd"); mysql_select_db("db"); $qr = mysql_query("select Test_Info FROM Memb_Info"); while ($row = mysql_fetch_array($qr)) { $tolist .= $row['Test_Info'] . ", "; } $subject = "hi"; $body = "nice"; $headers = "From: test@hotmail.com\r\n" . "X-Mailer: php"; foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time if ($currentemail, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } } // end foreach ?> Parse error: syntax error, unexpected ',' in /home/joran/domains/funzgames.com/public_html/mail/test.php on line 17 an error in this if ($currentemail, $subject, $body, $headers)) { I need some good tutorials ^^ I don't see the , error ??? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 26, 2008 Share Posted May 26, 2008 if (mail($currentemail, $subject, $body, $headers)) { Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 27, 2008 Author Share Posted May 27, 2008 then it says Warning: Invalid argument supplied for foreach() in /hom/html/mail/test.php on line 16. This means it's not an array? Quote Link to comment Share on other sites More sharing options...
.josh Posted May 27, 2008 Share Posted May 27, 2008 Oops. I set it up originally to just make a comma separated list. You need to change this: while ($row = mysql_fetch_array($qr)) { // this one make one long string of your emails, separated by commas $tolist .= $row['Test_Info'] . ", "; } to this: while ($row = mysql_fetch_array($qr)) { // this one makes an array of your emails $tolist[] = $row['Test_Info']; // this } Quote Link to comment Share on other sites More sharing options...
funzmu Posted May 27, 2008 Author Share Posted May 27, 2008 Allright it's working now... Great job, you helped me alot :) Here is the complete working script for people who need it also: <?php mysql_connect("localhost","sa","pwd"); //your sqlname, userlogin, password mysql_select_db("db"); //Database that need to be connected $qr = mysql_query("select monkeys FROM 1234"); //Select monkeys from table name 1234 while ($row = mysql_fetch_array($qr)) { $tolist[] = $row['monkey']; //fill in monkeys agian } $subject = "Hello"; $body = "Enjoy"; $headers = "From: everyone@hotmail.com\r\n" . "X-Mailer: php"; foreach ($tolist as $currentemail) { // loop to cycle through email addresses one at a time if (mail($currentemail, $subject, $body, $headers)) { echo("<p>Message sent!</p>"); } else { echo("<p>Message delivery failed...</p>"); } } // end foreach ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.