Php 迭代所有项目后执行邮件
我有以下问题。 对于循环的每个迭代,我都会收到单独的电子邮件。我希望邮件只发送一次,所有项目都重复Php 迭代所有项目后执行邮件,php,html,mysql,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 pre
<?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);
}
?>
试试这个。以这种方式编辑代码的最后一部分。在循环外获取邮件内容的一次性参数,让循环只生成邮件内容
$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);
$clean\u fullName=htmlspecialchars($fullName);
$clean_email=htmlspecialchars($email);
$to=$clean_电子邮件;
$to_us=”info@email.com";
$subject=“您的自定义资源包已到达!”;
$subject\u us=“新定制资源交付至:$clean\u email”;
$message='';
$message.=“”;
$message.=“嗨$clean_全名,
”;
$message.=“根据您的回复,我们创建了一个定制的资源包,以满足您的需求。
”;
foreach($数据作为$项){
$message.=“{$item['title']}
”;
$message.=“{$item['format']}
”;
$message.=“{$item['costs']}
”;
$message.=“{$item['link']}
”;
}
$message.=“如果您有任何问题,请随时联系我们。
”;
$message.=“”;
$message.='';
$message_us=“以下消息已发送至$clean_fullName
Hi$clean_fullName
”;
$message\u us.=“\r\n根据您的答复,我们已创建了一个定制的资源包,以满足您的需要:\r\n”;
$message\u us.=“\r\n如果您有任何问题,请随时联系我们。\r\n”;
$headers=“MIME版本:1.0”。“\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8”。“\r\n”;
$headers.='From:'。“\r\n”;
邮件($to、$subject、$message、$headers);
邮件($to_-us、$subject_-us、$message_-us、$headers);
使用此缩进,难怪您在理解它时遇到困难。您在此处给出的代码不是完整的代码。代码中还有其他循环吗?我们可以在代码的末尾看到}。从哪里开始?当然我会更新我的代码我已经对我的帖子进行了编辑。希望它更清楚