user2958298
user2958298

Reputation: 3

I need some help to fix a small thing in this php script

I have a website and I want to inform all the users (400 in total). To avoid mass mail restrictions on the server, someone helped me out with this script, where it delays the email sending. The problem here, is the $to, which I don't know what's that for, because the person that helped me forgot somehow about that. How can I fix this script to work well?

I'm in a kind of a hurry, because I don't know when it will be the next time to find that person again soon:D Thank you!

<?php
ini_set('max_execution_time', 600); //600 seconds = 10 minutes
$sql = "SELECT `email`FROM `users`";
$rs = mysql_query($sql) OR die("<pre>\n".$sql."</pre>\n".mysql_error());

while($sel = mysql_fetch_assoc($rs))
{
echo $sel['email'];
echo "<br>";

$headers = "From:site <[email protected]>\n";
$headers .= "Content-Type: text/plain;\r\n charset-iso-8859-1\r\n";
$body = "message";
$subject = "subject";
$recipient = $sel['email'];

foreach($to as $recipient)
{
$result = mail($recipient, $subject, $message, $header);
sleep(4);
}
}

Upvotes: 0

Views: 88

Answers (4)

Rachid
Rachid

Reputation: 832

First, you might have to add a space between the email and the FROM in the $sql statement to fix that query.

$sql = "SELECT `email` FROM `users`";

$recipient has to be either a comma separated list

$recipient = '';

while($sel = mysql_fetch_assoc($rs))
{
    $recipient .= $sel['email'] . ', '; // note the comma
}

$recipient = preg_replace('/, $/', '', $recipient); // remove last comma

$headers = "From: site <[email protected]>\n";
$headers .= "Content-Type: text/plain;\r\n charset-iso-8859-1\r\n";
$body = "message";
$subject = "subject";


$result = mail($recipient, $subject, $message, $header);
sleep(4);

Or you can loop through each recipient

$headers = "From: site <[email protected]>\n";
$headers .= "Content-Type: text/plain;\r\n charset-iso-8859-1\r\n";
$body = "message";
$subject = "subject";

while($sel = mysql_fetch_assoc($rs))
{
   $recipient = $sel['email'];    

   $result = mail($recipient, $subject, $message, $header);
   sleep(4);
}

You don't need the foreach($to as $recipient) because you're already in a loop. If you want to make you code work as is, then all you need is to switch the $to to $recipient in the foreach statement

$recipient = $sel['email'];

foreach($recipient as $to)
{
     $result = mail($to, $subject, $message, $header);
....

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

"The problem here, is the $to, which I don't know what's that for"

To explain your question about this ---^

foreach($to as $recipient) is (would be) valid if you had emails in an array.

For example:

$to = array('[email protected]', '[email protected]', 'email3@example');

I have a similar script that I wrote for testing purposes as a foreach

However this does not seem to be your case and is not needed.

You could also try:

foreach($sel as $recipient)

Instead of foreach($to as $recipient)

I say this because the code I use for a similar (working) application is this (simplified):

while($user = mysqli_fetch_array($query)){

  $to = $user['email'];
  mail($to, $subject, $body, $headers);

}

Upvotes: 1

Shreyans
Shreyans

Reputation: 186

$recipient is the array where you are individually storing the email addresses of everyone. So just interchange $to and $recipient in your foreach loop, and in the mail function replace $recipient with $to, and it should work just fine.

/* correct code */
foreach($recipient as $to) 
{
$result = mail($to, $subject, $message, $header); 
sleep(4);
}

Basically what you are doing is that you are first retreiving all the emails from the database and putting them in the array $recipient. When you use foreach after that then on each iteration, the value of the current element in $recipient is assigned to $to and the internal array pointer is advanced by one. This is why the first parameter in your mail function should be $to.

Upvotes: 1

skrilled
skrilled

Reputation: 5371

There is no $to variable declared, at any point in this script.

<?php
ini_set('max_execution_time', 600); //600 seconds = 10 minutes
$sql = "SELECT `email`FROM `users`";
$rs = mysql_query($sql) OR die("<pre>\n".$sql."</pre>\n".mysql_error());

while($sel = mysql_fetch_assoc($rs))
{
echo  $recipient = $sel['email'];
echo "<br>";

$headers = "From: site <[email protected]>\n";
$headers .= "Content-Type: text/plain;\r\n charset-iso-8859-1\r\n";
$body = "message";
$subject = "subject";


$result = mail($recipient, $subject, $message, $header);
sleep(4);

}

Upvotes: 3

Related Questions