2015-08-28 11 views
-2

我有以下問題。 我收到每個循環迭代的單獨電子郵件。我希望郵件只在迭代所有項目時才發送一次。執行郵件一旦所有項目已迭代

<?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 = "[email protected]"; 
     // 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: <[email protected]>' . "\r\n"; 

    } 

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

    } 


?> 

發生什麼是while循環到存儲在數組中的數據。該數組在foreach中使用。並且在這些循環之外,郵件假設郵寄結果。

在理論上這應該工作,但它不工作。

+0

有了這個縮進,難怪你無法搞清楚。 – e4c5

+0

你在這裏給出的代碼不是完整的代碼。你的代碼中有任何其他的循環?我們可以在代碼的最後看到}}。它從哪裏開始? –

+0

當然,我會更新我的代碼 – John

回答

0

試試這個。以這種方式編輯代碼的最後部分。在循環之外取一次郵件內容參數,讓循環只產生郵件內容。

$clean_fullName = htmlspecialchars($fullName); 
$clean_email = htmlspecialchars($email); 
$to = $clean_email; 
$to_us = "[email protected]"; 
$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: <[email protected]>' . "\r\n"; 


mail($to,$subject,$message,$headers); 
mail($to_us,$subject_us,$message_us,$headers); 
相關問題