I want to retrieve email from database and send message to all the email address.
I have written code for that but it is not working. I want someone to help me look at the code below and point out where am making mistakes and the correction.
The first page with code that retrieve all email from database and form to type the message
sendemail.php
<?php
//Connect to database
require('connect.php');
//Form for the listing all email and textarea for message
echo '<form action="sendmail.php" method="GET" id="sendemail" >';
//Setting up the variable
$mailcount = 0;
$namecount = 0;
//retriving all email from database
$result = mysql_query("SELECT * FROM mailinglist WHERE Send='1'");
while($row = mysql_fetch_assoc($result))
{
//listing all the emails as checkbox together with their names and the email
echo '<input type="checkbox" name="mail_'.$mailcount++.'" value="'.$row['Email'].'" CHECKED>'.$row['FirstName'].' '.$row['LastName'].' ('.$row['Email'].')
<input type="hidden" name="name_'.$namecount++.'" value="'.$row['FirstName'].'">
<br/>';
}
echo '<p>
<b> Message: </b><br/>
<textarea name="message" id="message"></textarea></p>
<input name="submit" type="submit" value="Send" />';
echo '</form>'
?>
Below is the second page that process the form contents and also send the message.
sendmail.php
<?php
require 'connect.php';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info@mydomain.com' . "\r\n";
$message = $_GET['message'];
//Loop through
for($x=0;$x<count($_GET);$x++)
{
if($_GET["mail_$x"])
{
//mail setup
$to = '$_GET["mail_$x"]' . "\r\n";
$subject = 'Testing Email';
$body = "Dear ".$_GET["name_$x"]."
\n\n $message \n\n
YESHUA SCHOOL.";
mail($to, $subject, $body, $headers);
//break;
if(mail($to, $subject, $body, $message))
{
echo 'Message sent successfully';
}
else {
echo 'Message failed';
}
}
//break;
}
?>
My problem is that, when I tested it with wampserver, I will get these errors
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\dashboard\sendmail.php on line 38
Message failed
Notice: Undefined index: mail_3 in C:\wamp\www\dasboard\sendmail.php on line 26
Notice: Undefined index: mail_4 in C:\wamp\www\dasboard\sendmail.php on line 26
Notice: Undefined index: mail_5 in C:\wamp\www\dasboard\sendmail.php on line 26
Notice: Undefined index: mail_6 in C:\wamp\www\dasboard\sendmail.php on line 26
Notice: Undefined index: mail_7 in C:\wamp\www\dasboard\sendmail.php on line 26
When I used break; to stop the (for loop), I won't see the undefined index error.
But when I upload the code to live server and tested, I got only the Message failed with any error message.
Please someone should help me out.