tags:

views:

38

answers:

3

I'd like to know how to send mails to users who is already stored in my database so I want to select all from my database table and send them a mail and also is I want to send to the selected emails only how that can be done

This is the relevant code of the admin interface:

<?php 

        $get_U_data = " select * from maling_list ";
        $result = $db -> query ($get_U_data) or die ($db->error);
        if ($result) {
?>
<h2>Send your newsletter</h2>

<form action="mailit.php" method="post" >
Category:
<select name="category">
        <option value="1">option1</option>
        <option value="2">option2</option>
        <option value="3">option3</option>
        <option value="4">option4</option>
</select>
<select name="select" size="15" multiple="multiple" id="select">
      <option>--------------</option>
      <?php 
      while ($row = $result -> fetch_object()) {
      ?>
      <option><?php echo $row->company ?><br /></option>
<?php
      }
}
?>
      <option>--------------</option>
</select><br />
Subject: <input type="text" name="subject" /><br />
Message<: <textarea name="body" cols="60" rows="15"></textarea><br>
<input type="submit" name="submit" value="Send" />
</form>

please I need help on this

this is my new code

<?php
  include_once("../admin_config/config.php");
  $getMails = " select * from maling_list where received = 0 limit 20 ";
  $result = $db->query($getMails) or die($db->error);
  $dbfailures = array();
  $failures = array();
  $success = array();
  while ($row = $result->fetch_array()) {
      $email = $row['email'];
      $name = $row['company'];
      $subject = $_POST['subject'];
      $cat = $_POST['category'];
      $mailbody = $_POST['body'];
      $headers = "From : [email protected]\r\n";
      $to = "$email";
      $mailResult = mail($to, $subject, $mailbody, $cat, $headers);
      if ($mailResult) {
          $updataData = " UPDATE mailing_list SET received = '1' where email = '" . $db->real_escape_string($email) . "' LIMIT 1";
          $resultUpdate = $db->query($updataData) or die($db->error);
          if ($resultUpdate) {
              $success[] = $email;
          } else {
              $dbfailures[] = $email;
          }
      } else {
          $failures[] = $email;
      }
  }
  echo "These mails didn't get sent: " . htmlentities(implode(', ', $failures)) . "<br />" . "These mails didn't get updated in db: " . htmlentities(implode(', ', $dbfailures)) . "<br />" . "These mails were successfully sent: " . htmlentities(implode(', ', $success));
?>
A: 

run the mail function in a loop

THOmas
I think I did this please look at my new code at the top I added my post thank you my friend Oh I forget to tell that I am using Mysqli not Mysql
Yousef Altaf
A: 
  1. Fetch the company names from the POST request
  2. Look it up in the database
  3. Iterate through all e-mail addresses and send a mail.

Example code:

$subject = filter_input(INPUT_POST, 'subject');
$message = filter_input(INPUT_POST, 'message');
$escaped_names = array();
foreach($_POST['select'] as $email){
   $escaped_names[] = mysql_real_escape_string((string)$email);
}
$sql = "SELECT email FROM mailing_list WHERE company IN ('" . implode("','", $escaped_names) . "')";
$query = mysql_query($sql);
if($query && mysql_num_rows($query)){
   while($row=mysql_fetch_array($query)){
      mail($row['email'], $subject, $message);
   }
}

<?php
  include_once("../admin_config/config.php");
  $getMails = " select * from maling_list where received = 0 limit 20 ";
  $result = $db->query($getMails) or die($db->error);

  $subject = $_POST['subject'];
  $cat = $_POST['category'];/* what is this for? Consider adding it to $mailbody */
  $mailbody = $_POST['body'];
  $headers = "From : [email protected]\r\n";
  $headers .= "Content-type: text/html\r\n";

  // added the following
  $dbfailures = array();
  $failures = array();
  $success = array();

  while ($row = $result->fetch_array()) {
      $email = $row['email'];
      $name = $row['company'];

      $to = "$email";

      $mailResult = mail($to, $subject, $mailbody, $headers);
      if ($mailResult) {// if the mail is successfully sent
          $updataData = " UPDATE mailing_list SET received = '1' where email = '" . $db->real_escape_string($email) . "' LIMIT 1";
          $resultUpdate = $db->query($updataData) or die($db->error);
          if ($resultUpdate) {
              $success[] = $email;
          } else {
              // you could exit the script after 5 database failures
              $dbfailures[] = $email;
          }
      }
      else{
         // these mails don't get sent
         $failures[] = $email;
      }
  }
  echo "These mails didn't get sent: ".htmlentities(implode(', ', $failures))."<br />".
       "These mails didn't get updated in db: ".htmlentities(implode(', ', $dbfailures))."<br />".
       "These mails were successfully sent: "htmlentities(implode(', ', $success));
?>
Lekensteyn
Yesterday how are you my friend I am glad found someone can help me in my issue, Yesterday I have read your code carefully but before you send this code to me I have come out with this code and I'd like you to discuss with me
Yousef Altaf
"Repaired" your code.
Lekensteyn
Thanks Lekensteyn this was what I want I will try it and come back to you Thanks very much Lekensteyn I'd like the way you explain the code to me ... and thanks for all the friend who try to help
Yousef Altaf
Lekensteyn how are you my friend I trying to get this to work but it doesn't want to work I don't know why I did like you told me but still return empty like this!!These mails didn't get sent:These mails didn't get updated in db:These mails were successfully sent:can you tell me what is the problem now
Yousef Altaf
Does you table have a row called `email`? You should not literally copy and paste this code. Please take a look at the comments (`/* what does this line do? */`).
Lekensteyn
OK sire I did look at the comments (/* what does this line do? */). and I changed my code again please take a look at my new code above. Yes my database has row called email
Yousef Altaf
Please ident your code correctly! You can use [PHP Formatter](http://www.phpformatter.com/) for that. Something is wrong with `$mailResult = mail($to, $subject, $mailbody, $cat, $headers);`, you should remove the `$cat` parameter. (see [`mail()`](http://nl2.php.net/manual/en/function.mail.php)). Since `$subject`, `$cat`, `$mailbody` and `$headers` are static, you should place them outside the while loop.
Lekensteyn
Hay I got it to work lot of thanks Lekensteyn and Martin Holzhauer THOmas and all my friends they try to help me by the way I want to say where was my error it was very simple error ('0' radar then 0) in this line ($getMails = " select * from maling_list where received = 0 limit 20 ";)the zero for received has to be like this '0' so the all code is like this ($getMails = " select * from maling_list where received = '0' limit 20 ";) Thank you again my friends
Yousef Altaf
If you found an answer hepful, use the arrow button to rate it. If the answer is actually solving your question, press the Accept button.
Lekensteyn
A: 

you may want to have a look at this - http://www.php.net/manual/en/function.mail.php#85476

<?php

$mailbody = "";
$subject = "Egindex newsletter";
$headers.= "From : [email protected]\r\n";
$headers.= "Content-type: text/html\r\n";
$to = '';

$headers.="Bcc: ";
while ($row = $result->fetch_array()) {
    $headers.=$row['email'].", ";
}
$headers.="[email protected]\r\n";

$mailResult = mail($to, $subject, $mailbody, $headers);

?>
Martin Holzhauer