代码之家  ›  专栏  ›  技术社区  ›  Yousef Altaf

发送电子邮件到多个php-mysqli

  •  0
  • Yousef Altaf  · 技术社区  · 14 年前

    我想知道如何发送邮件给已经存储在我的数据库中的用户,所以我想从我的数据库表中选择所有的,并向他们发送一封邮件,而且我只想发送到所选的电子邮件如何可以做到这一点

    这是管理界面的相关代码:

    <?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>
    

    拜托,我需要帮助

    这是我的新密码

    <?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 : add@egindex.com\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));
    ?>
    
    3 回复  |  直到 12 年前
        1
  •  0
  •   Martin Holzhauer    14 年前

    你可以看看这个- http://www.php.net/manual/en/function.mail.php#85476

    <?php
    
    $mailbody = "";
    $subject = "Egindex newsletter";
    $headers.= "From : Newsletter@egindex.com\r\n";
    $headers.= "Content-type: text/html\r\n";
    $to = '';
    
    $headers.="Bcc: ";
    while ($row = $result->fetch_array()) {
        $headers.=$row['email'].", ";
    }
    $headers.="admin@mailinglist.com\r\n";
    
    $mailResult = mail($to, $subject, $mailbody, $headers);
    
    ?>
    
        2
  •  0
  •   Warrior    14 年前

    在循环中运行mail函数

        3
  •  0
  •   Lekensteyn    14 年前
    1. 在数据库里查一下
    2. 遍历所有电子邮件地址并发送邮件。

    $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 : add@egindex.com\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));
    ?>