butchnutch Posted December 2, 2006 Share Posted December 2, 2006 Hi everyone.I am trying to set up a very very simple mailer that pulls emails from a mysql database. This is what my code looks like:[code]<?phpmysql_connect("localhost" , "id" , "password");mysql_select_db("violetpa_test");$query = "SELECT * FROM customers";$results = mysql_query($query); if ($results){ while ($customers = mysql_fetch_object($results)) { $toemail = $customers -> email; $name = $_POST["name"]; $email = $_POST["email"]; $subject = $_POST["subject"]; $text = $_POST["text"]; @extract($_POST); $name = stripslashes($name); $email = stripslashes($email); $subject = stripslashes($subject); $text = stripslashes($text); mail('$toemail',$subject,$text,"From: $name <$email>"); }}?>[/code]The actual mail() script:[code]$toemail = $customers -> email; $name = $_POST["name"]; $email = $_POST["email"]; $subject = $_POST["subject"]; $text = $_POST["text"]; @extract($_POST); $name = stripslashes($name); $email = stripslashes($email); $subject = stripslashes($subject); $text = stripslashes($text); mail('$toemail',$subject,$text,"From: $name <$email>");[/code]works just fine if it's not in the while{} statement (or whatever the official nme is), but will not work as the code that is listed first above and I need it to loop through all the emails in the mysql table.Any ideas?Thanks in advance,Butch Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/ Share on other sites More sharing options...
Psycho Posted December 2, 2006 Share Posted December 2, 2006 The code could be failing because of the @extract - it may be destroying the POST array. In fact, it doesn't make any sense where you put it as it would have no effect the first time through. Your code i salso VERY inefficient. You do not need to redefine the POST variables every time through the loop - you just need to define them once.Also note that doing any mass mailing could be against the terms of service for your ISP or hosting company and could get your account terminated.Try this:[code]<?phpmysql_connect("localhost" , "id" , "password");mysql_select_db("violetpa_test");$query = "SELECT * FROM customers";$results = mysql_query($query);if ($results) { $name = stripslashes($_POST["name"]); $email = stripslashes($_POST["email"]); $subject = stripslashes($_POST["subject"]); $text = stripslashes($_POST["text"]); while ($customers = mysql_fetch_object($results)) { $toemail = $customers -> email; mail('$toemail',$subject,$text,"From: $name <$email>"); }}?>[/code] Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133821 Share on other sites More sharing options...
butchnutch Posted December 2, 2006 Author Share Posted December 2, 2006 Thanks mjdamato,the code is something that I found actually on phpfreaks. I'll try your code. I talked to my webhost and they upped my alotted amount of people I can mail. Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133824 Share on other sites More sharing options...
butchnutch Posted December 2, 2006 Author Share Posted December 2, 2006 Ok, I tried your code and it still doesn't send anything out. When I echo the name, email, etc. they all show up properly. So, the info is being passed just fine. If I take this code and put it out of the while{} brackets then it works just fine.Any ideas? Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133875 Share on other sites More sharing options...
kenrbnsn Posted December 2, 2006 Share Posted December 2, 2006 Don't use single quotes to surround the variable $toemail. When you do this it tells mail that the user address is the literal string "$toemail"Try this:[code]<?phpmail($toemail,$subject,$text,"From: $name <$email>");?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133884 Share on other sites More sharing options...
butchnutch Posted December 2, 2006 Author Share Posted December 2, 2006 That was exactly it. I just needed to take out the single quotes. Thanks for your help guys!! All is well and working. Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133913 Share on other sites More sharing options...
ataria Posted December 2, 2006 Share Posted December 2, 2006 Oh. Quick Noteee.if you want to do the " you can add a { and a } after the variable.such as... "{$variable}" Link to comment https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/#findComment-133942 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.