Liam-a- Posted October 29, 2013 Share Posted October 29, 2013 Can anyone tell me what is wrong with this script? <?php//db connectionmysql_connect($db_host,$db_username,$db_password) or die(mysql_error());mysql_select_db($db_name) or die(mysql_error());if ($submit) {//set emails as array$emails = array();$x = 1;//quantity of emails sent before 3 sec delay to avoid timeout$hold = 99;//query to select the email address$sql = mysql_query("SELECT email_address FROM tblusers") or die(mysql_error());//fetch themwhile($r = mysql_fetch_array($sql)) {$emails[] = $r['email'];}//count total emails$total = count($emails);//repeat for each emailfor ($i = 1; $i <= $total; $i++) {//stripslashes in message$msg = stripslashes($msg);//send email to user (need to change name and email:)mail($emails[$i], $subject, $msg, "From: Your Site<newsletter@yoursite.com>");//if $x = 10 then wait 3 seconds to avoid timeout$x++;if($x == $hold) {sleep(3);$x = 0;}}//close database connection and echo success messagemysql_close();echo "<strong>Mail successfully sent to $total addresses!</strong><br><br>";}?><html><head><title>Send Mail</title><body><form name="mail" action="<? echo $PHP_SELF; ?>" method="post">Subject: <input type="text" name="subject" size="40"><br><textarea name="msg" cols="40" rows="8"></textarea><br><input type="submit" name="submit" value="Send Email"></form></body></html> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 29, 2013 Share Posted October 29, 2013 Why what issue / error have you got...? Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 29, 2013 Author Share Posted October 29, 2013 when i submit nothing happens, doesnt tell me how many are sent just click and refreshes page basicly Quote Link to comment Share on other sites More sharing options...
Rifts Posted October 29, 2013 Share Posted October 29, 2013 (edited) It might be a problem with your host. I was using godaddy as my host and trying to use php to send mass emails but godaddy limits the amount you can send to a very very small number. I think i was able to sent like 5 emails before I had to wait like 24 hours. just a thought. also you might want to just try sending a single email to yourself just to make sure you can send emails through php. something like mail( 'Your Email', 'Test Email', 'Testing send php mail, "From: Your Site<newsletter@yoursite.com>"); Edited October 29, 2013 by Rifts Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 29, 2013 Share Posted October 29, 2013 Probably timing out then! Print out after each email to test... oooh especially with the sleep() Do somany then redirect back to itself passing where to start... do it in batches bascially. Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 mail($emails[$i], $subject, $msg, "From: Your Site<newsletter@yoursite.com>"); This line I changed $emails[$i] to my email, and it sent me emails fine, can anyone help me why this "$emails[$i]" is causing a 500 error. Thanks Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 mail($emails[$i], $subject, $msg, "From: Your Site<newsletter@yoursite.com>"); This line I changed $emails[$i] to my email, and it sent me emails fine, can anyone help me why this "$emails[$i]" is causing a 500 error. Thanks even putting this in "" still gives me a error and no emails sent Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 30, 2013 Share Posted October 30, 2013 If you echo it out what is in it (the offending one)? Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 If you echo it out what is in it (the offending one)? What do you mean? Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 30, 2013 Share Posted October 30, 2013 You say this ($emails[$i]) is where the error comes from, so print it to screen to check it... Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted October 30, 2013 Share Posted October 30, 2013 (edited) GLOBAL VARIABLES ARE BEING USED! RULE 101 IN PHP: DO NOT USE GLOBAL VARIABLES. Have you written this script or copied & pasted it from somewhere? Here is a modified version: <?php //db connection mysql_connect($db_host,$db_username,$db_password) or die(mysql_error()); mysql_select_db($db_name) or die(mysql_error()); if(isset($_POST['submit'])) { //set emails as array $emails = array(); $x = 1; //quantity of emails sent before 3 sec delay to avoid timeout $hold = 99; //query to select the email address $sql = mysql_query("SELECT email_address FROM tblusers") or die(mysql_error()); //fetch them while($r = mysql_fetch_array($sql)) { $emails[] = $r['email']; } //count total emails $total = count($emails); //repeat for each email for ($i = 1; $i <= $total; $i++) { //stripslashes in message $msg = stripslashes($_POST['msg']); //send email to user (need to change name and email:) mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>"); //if $x = 10 then wait 3 seconds to avoid timeout $x++; if($x == $hold) { sleep(3); $x = 0; } } //close database connection and echo success message mysql_close(); echo "<strong>Mail successfully sent to $total addresses!</strong><br><br>"; } ?> <html> <head> <title>Send Mail</title> <body> <form name="mail" action="" method="post"> Subject: <input type="text" name="subject" size="40"><br> <textarea name="msg" cols="40" rows="8"></textarea><br> <input type="submit" name="submit" value="Send Email"> </form> </body> </html> On a side note, you should not be using a web browser script to send mass email. This should be done through a command line script. You should use a SMTP server to send the mail. PHP's mail() function is not sufficient enough. Have a look at PEAR::Mail http://pear.php.net/package/Mail Edited October 30, 2013 by neil.johnson Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 You say this ($emails[$i]) is where the error comes from, so print it to screen to check it... When i press submit it gives me Error 500 Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 30, 2013 Share Posted October 30, 2013 Add the following to the top of your script to see if you get a better error message: error_reporting(E_ALL); ini_set('display_errors',E_ALL); Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 Add the following to the top of your script to see if you get a better error message: error_reporting(E_ALL); ini_set('display_errors',E_ALL); Already had that in still giving error 500 Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 30, 2013 Share Posted October 30, 2013 OK, show your current code (preferably in a code block without so many blank lines ) Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted October 30, 2013 Author Share Posted October 30, 2013 OK, show your current code (preferably in a code block without so many blank lines ) Theres a funny thing with this code, i press submit and it goes straight to error 500 but still send out ALL emails . so if its only going to be me using this script im not fussed if it returns to a error page after but still does the job. Thanks. 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.