1.11M Members

How to Send Mail to Email Address on Mysql Database?

 
0
 

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.

 
0
 

Have you checked you php.ini file to see whether you SMTP address and port details are correct? I can't see line 38 in sendmail.php but that's the first place I would look according to your error message.

 
0
 

I can't see line 38 in sendmail.php but that's the first place I would look according to your error message.

You mis-read a part of the error wrong. The server displayed an error to check SMTP configuration details in php.ini, The server also suggested him to dynamically add SMTP settings to the script using ini_set() on the page.

--

@deyesborn, as StephNicolaou suggested, you should have a look into your SMTP settings. either change your php ini or add them on run by using ini_set();

 
0
 

I was able to figure out mistakes I made and it is working fine now. But the problem am having is that, when I send mail, two mails will be sent. One from my email I added to the send field and the other one from my cpanel username and host mailserver.

What is causing that problem?

 
1
 

not sure, maybe I think its sending twice because of the code you're using?

$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';
       }

try changing it to

$body = "Dear ".$_GET["name_$x"]."
        \n\n $message \n\n
       YESHUA SCHOOL.";

       if(mail($to, $subject, $body, $message))
       {
           echo 'Message sent successfully';
       }
 else {
           echo 'Message failed';
       }

maybe that would work. I'm not sure, but its worth giving a try.

 
0
 

emclondon is correct. Chage your code as per the his suggestion. the mail will bw send only once.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: