所有项目都被迭代后执行邮件

时间:2015-08-28 09:59:37

标签: php html mysql

我有以下问题。 我为循环的每次迭代收到单独的电子邮件。我希望邮件只发送一次,迭代所有项目。

  <?php


  // Honey pot trap
  // Create a hidden input that is only visible to bots. If it's empty than proceed.
  if (empty($_POST['humancheck'])){
    // Proceeed if submit button have been pressed


      $fullName = $_POST['fname'];
      $email = $_POST['email'];
      $stage = $_POST['stage'];


      include("db.php");

      $resources = "select * from resources where stage LIKE '%".$stage."%'";

      $run_query = mysqli_query($con, $resources);


      while($row = mysqli_fetch_array($run_query)) {

        $data[] = array(
          'format' => $row['format'],
          'title' => $row['title'],
          'costs' => $row['cost'],
          'stage' => $row['stage'],
          'topic' => $row['topic'],
          'link' => $row['link']
        );
      }

      foreach($data as $item) {
        // Sanitize input data
        $clean_fullName = htmlspecialchars($fullName);
        $clean_email = htmlspecialchars($email);

        // Mail Set up
        $to = $clean_email;
        $to_us = "info@email.com";
        // Email subject
        $subject = "Your custom resource pack has arrived!";
        $subject_us = "New custom resource delivered to: $clean_email";

        $message = '<html><body>';
        $message .= "<p>";
        $message .= "Hi $clean_fullName, <br><br>";
        $message .= " Based on your responses, we have created a custom resource pack tailored to your needs.  <br><br>";
        $message .= "<b>{$item['title']}</b><br>";
        $message .= "{$item['format']} <br>";
        $message .= "{$item['costs']} <br>";
        $message .= "{$item['link']} <br><br>";

        $message .= " If you have any questions, do not hesitate to reach out to us.  <br><br>";
        $message .= "</p>";
        $message .= '</body></html>';

        $message_us = "The below message was sent to $clean_fullName <br>
                      <i>  Hi $clean_fullName <br>";
        $message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs:  \r\n";
        $message_us .= "\r\n If you have any questions, do not hesitate to reach out to us.  \r\n";


        // Headers
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= 'From: <info@email.com>' . "\r\n";

    }

    mail($to,$subject,$message,$headers);
    mail($to_us,$subject_us,$message_us,$headers);

  }


?>

发生的事情是while循环存储在数组中的数据。该数组用于foreach。在循环之外,邮件被假定为邮寄结果。

从理论上讲,这应该有效但不起作用。

1 个答案:

答案 0 :(得分:0)

试试这个。以这种方式编辑代码的最后一部分。在循环外部取一次邮件内容参数,让循环只制作邮件内容。

$clean_fullName = htmlspecialchars($fullName);
$clean_email = htmlspecialchars($email);
$to = $clean_email;
$to_us = "info@email.com";
$subject = "Your custom resource pack has arrived!";
$subject_us = "New custom resource delivered to: $clean_email";
$message = '<html><body>';
$message .= "<p>";
$message .= "Hi $clean_fullName, <br><br>";
$message .= " Based on your responses, we have created a custom resource pack tailored to your needs.  <br><br>";

foreach($data as $item) {

$message .= "<b>{$item['title']}</b><br>";
$message .= "{$item['format']} <br>";
$message .= "{$item['costs']} <br>";
$message .= "{$item['link']} <br><br>";

}

$message .= " If you have any questions, do not hesitate to reach out to us.  <br><br>";
$message .= "</p>";
$message .= '</body></html>';
$message_us = "The below message was sent to $clean_fullName <br><i>  Hi $clean_fullName <br>";
$message_us .= "\r\n Based on your responses, we have created a custom resource pack tailored to your needs:  \r\n";
$message_us .= "\r\n If you have any questions, do not hesitate to reach out to us.  \r\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <info@email.com>' . "\r\n";


mail($to,$subject,$message,$headers);
mail($to_us,$subject_us,$message_us,$headers);