Liam-a- Posted November 8, 2013 Share Posted November 8, 2013 Hello, I currently have this script, its sending emails perfect but am getting a Error-500: Internal server error. after i press the submit button. its strange how i get this error but the mail still sends, ive tried for days to stop the error can anyone on here help me get rid of the error and help me to be able to send this message in HTML as I want to add images. well i would like to create the HTML email body within the code if anyone can point me in the right direction please. <?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> Thanks Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 8, 2013 Author Share Posted November 8, 2013 Also is there a PHP Email header you can add so it wont go to SPAM box? Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 8, 2013 Author Share Posted November 8, 2013 Can anyone help please Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 8, 2013 Share Posted November 8, 2013 You can not add some magic header to not go to SPAM. Every spammer would add it. I'll look for the error. Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 8, 2013 Author Share Posted November 8, 2013 You can not add some magic header to not go to SPAM. Every spammer would add it. I'll look for the error. Just alot of sites when they send out there spam mail it comes to inbox :/ not spam Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 8, 2013 Share Posted November 8, 2013 For the 500 error make sure you are displaying php errors. Put this at the top of the script: ini_set("display_errors", "1"); error_reporting(-1); If that doesn't work try the directions here: http://www.cyberciti.biz/tips/http-error-500-internal-server-for-php-pages-and-solution.html Instead of looping through the mysql_fetch_arrya and building a huge array of addresses and then sending, why not just read one, send it and read the next. Also, if you are writing something new may I suggest using mysqli which is very similar but likely to work longer because the mysql_ functions are deprecated (being phased out). Quote Link to comment Share on other sites More sharing options...
DjordjeB Posted November 9, 2013 Share Posted November 9, 2013 i think you need to make some idle function before you send next mail. becouse some servers has limit send mail for 30 sec example: 50mail for 10 sec Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 10, 2013 Share Posted November 10, 2013 i think you need to make some idle function before you send next mail. becouse some servers has limit send mail for 30 sec example: 50mail for 10 sec That is what he does here: if($x == $hold) { sleep(3); $x = 0; } Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 11, 2013 Author Share Posted November 11, 2013 Ive found a new error on Line 30. PHP Warning: mail(): SMTP server response: 503 Bad sequence of commands. You must specify the recipients of a message before you can send it in Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 11, 2013 Author Share Posted November 11, 2013 Ive found a new error on Line 30. PHP Warning: mail(): SMTP server response: 503 Bad sequence of commands. You must specify the recipients of a message before you can send it in Line 30: mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>"); Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 11, 2013 Author Share Posted November 11, 2013 Can anyone help? am getting a error its about Function mail() PHP Warning: mail(): SMTP server response: 503 Bad sequence of commands. You must specify the recipients of a message before you can send it in Line 30: mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>"); Now when ive been on the php website people say mail is the correct function to use, now my windows server rejects this.. can anyone help me out with a function that will send mail and also work on windows server. Thanks Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 12, 2013 Share Posted November 12, 2013 Can anyone help? am getting a error its about Function mail() PHP Warning: mail(): SMTP server response: 503 Bad sequence of commands. You must specify the recipients of a message before you can send it in Line 30: mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>"); Now when ive been on the php website people say mail is the correct function to use, now my windows server rejects this.. can anyone help me out with a function that will send mail and also work on windows server. Thanks looks from the error message like you have an empty or bad $email[$i]. Try adding some code to the bad line to see what is going on: if (mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>")) { echo 'Mail '.$i.' sent'; }else{ echo "Mail $i failed<br>To: ".$emails[$i].'<br>'; } Quote Link to comment Share on other sites More sharing options...
Liam-a- Posted November 12, 2013 Author Share Posted November 12, 2013 Line 83: if (mail($emails[$i], $_POST['subject'], $msg, "From: Your Site<newsletter@yoursite.com>")) Line 87: echo "Mail $i failed<br>To: ".$emails[$i].'<br>'; New error PHP Notice: Undefined offset: 3 in Email.php on line 83PHP Warning: mail(): SMTP server response: 503 Bad sequence of commands. You must specify the recipients of a message before you can send it in Email.php on line 83PHP Notice: Undefined offset: 3 in Email.php on line 87 Don't no what these errors mean Quote Link to comment Share on other sites More sharing options...
davidannis Posted November 13, 2013 Share Posted November 13, 2013 The undefined offset means that you are pointing to an array element that does not exist. I think that is because you do this: $total = count($emails); //repeat for each email for ($i = 1; $i <= $total; $i++) but arrays start at element 0 (not 1) so you should not start $i at zero not one and $i should go to <$total not <= $total. 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.