Jump to content

PHP Mass Email Script


Liam-a-

Recommended Posts

Can anyone tell me what is wrong with this script?

 

<?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 (
$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($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 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="<? 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>

Link to comment
Share on other sites

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 by Rifts
Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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 by neil.johnson
Link to comment
Share on other sites

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.

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.