mail() error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bcart
Forum Newbie
Posts: 10
Joined: Fri Oct 30, 2009 8:09 am

mail() error

Post by bcart »

Hi there

I'm trying to bulk email people at work with the script below. It works fine when I actually type 1 email address against the $to variable but when I assign $to against a loop through all of the ones I want to then it produces the following error

"Warning: mail() [function.mail]: SMTP server response: 550 Relaying denied in C:\xampp\htdocs\intranet\staff_satisfaction\mailer.php on line 51"

The code I'm using is this

<?php

if (isset($_POST['submitted'])) {

$q = "SELECT * FROM ss_staff";
$r = mysqli_query($dbc, $q);
$row = mysqli_fetch_array($r);

do {

$recipients .= '\'' . $row['email'] . '@totalpeople.co.uk\', ';

} while ($row = mysqli_fetch_array($r));//end of while loop

$to = substr($recipients, 0, -2);

$body = "body of email";

$email = 'email@domain.co.uk';

$subject = 'Subject of email';
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";

// Send the email:
$sendMail = mail($to, $subject, $body, $headers);
//mail($to, $subject, $body, $headers);

if ($sendMail) {

echo '<h2>Congratulations! You have successfully mailed everyone.</h2>';
exit();

} else {

// Public message:
echo "<h1>System Error</h1>
<p class=\"error\">This didn't work. $to</p>";

// Debugging message:
echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p><br /><br /><br /><br />';
exit(); // Quit the script.

}

} else {

echo '<form action="mailer.php" method="post">

<input type="submit" name="submit" value="Submit" />
<input type="hidden" name="submitted" value="TRUE" />

</form>';

}//end of main if

?>

Can anyone help?

Thanks!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: mail() error

Post by Jonah Bron »

Your While loop is structured incorrectly. With this configuration, it will loop at least once, even if the table is empty. If you surround your code with [syntax=php][/syntax] tags (click the edit button on your post), you will see that there are problems with the assignment of the $recipients variable. Turn on PHP errors in your php.ini config file, and run the code again.
Post Reply