perezf Posted August 13, 2007 Share Posted August 13, 2007 Im having a problem with a mailing list i need to make for someone, I have to have the same email sent to all the emails in the database what am i doing wrong <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); mysql_fetch_assoc($result); for($i=0; $i<=1000; $i++) { echo "Emailing: $i"; mail($result['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: testing@test.com") or die("Could not send email"); echo "Email Successfully Sent to".$contact['emailaddr']; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/ Share on other sites More sharing options...
phpknight Posted August 13, 2007 Share Posted August 13, 2007 What it is actually doing, though? Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322120 Share on other sites More sharing options...
perezf Posted August 13, 2007 Author Share Posted August 13, 2007 I solved it <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); $row = mysql_fetch_assoc($result); $nrows = mysql_num_rows($result); for($i=1; $i<=$nrows; $i++) { mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email"); echo "Email Successfully Sent to: ".$row['emailaddr']."<br />"; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322121 Share on other sites More sharing options...
cunoodle2 Posted August 13, 2007 Share Posted August 13, 2007 Your code would only send to 1 person (but the person would be e-mailed 1,000 times). I'm a little concerned with help here as I just want to make sure that you are not spamming. This concern comes from the loop maxing out at 1,000 (meaning that you are just gonna send 1,000) e-mails without any concern for how many people are actually on your mailing list. In any case here is some code broken up and explained line by line... //create the mysql query $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); //this sends the actual query to the database and stores the entire result in the "$result" variable //note that this will have to be broken up later into individual lines $result = mysql_query($query); //this just counts the actual number of matches from the query $num_results = mysql_num_rows($result); //create and initialize a counter $count = 0; //this for loop uses the number of matches and then goes through them one at a time for($i = 0; $i < $num_results; $i++) { //this stores a single row of results in the variable "$row" $row = mysql_fetch_array($result); //increase count number and print to screen $count++; echo "<b>Emailing user number:</b> $count <br />"; //generate and send e-mail mail($row["emailaddr"], "Test from Frank", $_POST['emailcontents'], "From: testing@test.com") or die("Could not send email"); echo "Email Successfully Sent to ".$row["emailaddr"]."<br /><br />\n"; } //end of for($i = 0; $i < $num_results; $i++) If this code doesn't work then you need to look specifically at the name of the database and/or the column name(s). Your code would be more efficient as well if instead of using the "SELECT * FROM emails" you used a specific column name like "SELECT emailaddr FROM emails" Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322122 Share on other sites More sharing options...
cunoodle2 Posted August 13, 2007 Share Posted August 13, 2007 Sorry... while I was writing the code for my response you had already solved it. Hopefully this helps someone else at the very least =). Also for future reference please make detailed notes as to what the fault(s) were with your code and exactly what you did to change them. This will allow future users to learn easier from these forums =) Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322124 Share on other sites More sharing options...
perezf Posted August 13, 2007 Author Share Posted August 13, 2007 Im having a new problem, now how do i make it go through my list of emails, right now all the emails go to just one email <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); $row = mysql_fetch_assoc($result); $nrows = mysql_num_rows($result); for($i=1; $i<=$nrows; $i++) { mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email"); echo "Email Successfully Sent to: ".$row['emailaddr']."<br />"; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> <a href="addemail.php">Add an Email to the List</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322126 Share on other sites More sharing options...
perezf Posted August 13, 2007 Author Share Posted August 13, 2007 thank you cunoodle2 I read through what you wrote, and it helped me out a great deal Thank you Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322133 Share on other sites More sharing options...
cunoodle2 Posted August 13, 2007 Share Posted August 13, 2007 You have this marked as "SOLVED" yet you still have a question so I'm going to assume that you mistakenly marked it and have yet to figure out the issue... The problem with your code is that you only fetch a row from the result 1 time before the loop. You need to also fetch a row in the loop so as it loops through it grabs the very next email in the list. So... either used the code that I already provided you OR you could modify your loop to look like this... <?php for($i=1; $i<=$nrows; $i++) { mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@spacenetmedia.com") or die("Could not send email"); echo "Email Successfully Sent to: ".$row['emailaddr']."<br />"; $row = mysql_fetch_assoc($result); } ?> Let me know if you need anything else. Quote Link to comment https://forums.phpfreaks.com/topic/64607-solved-emailing-problem/#findComment-322701 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.