Jump to content

PHP Mass Email Help


Liam-a-

Recommended Posts

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

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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>");

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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>';
}
Link to comment
Share on other sites

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 83
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 Email.php on line 83
PHP Notice: Undefined offset: 3 in Email.php on line 87
 
Don't no what these errors mean
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.