在phpmailer消息中循环?

时间:2013-09-09 08:27:24

标签: php html phpmailer

我有一个表格显示每行的复选框,用户可以选择多个复选框,然后单击提交,然后使用phpmailer发送电子邮件。 我希望用户选择的项目包含在电子邮件正文中。我知道下面的内容有误,但有人可以告诉我该怎么做吗?

这是我到目前为止...它确实发送了一封电子邮件,但只有数组中的第一个选择在正文中。

$select = $_POST['select'];
if(empty($select)) 
{
 echo("You didn't make any selections.");
} 
else
{
$count = count($select);
 for($i=0; $i < $count; $i++)
{

$to = "test@test.com";
$subject = "Booking Reguest";
$message = '    
<html>
<head>
<title>Booking Request</title>
</head>
<body>
<p>Please create bookings for the following ...</p>
<p>'. $select[$i] .',</p>
<p>Kind regards</p>
<p>blah blah blah</p>
</body>
</html>';
$headers = 'From: noreply@test.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to,$subject,$message,$headers);
    }
    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

也许您正在循环整个邮件而不是仅选择?

请向我们提供您的HTML代码,但是,如果我已经理解了问题,这将解决它:

$select = $_POST['select'];
if(empty($select)) 
{
 echo("You didn't make any selections.");
} 
else
{
$count = count($select);

$to = "test@test.com";
$subject = "Booking Reguest";
$message = '    
<html>
<head>
<title>Booking Request</title>
</head>
<body>
<p>Please create bookings for the following ...</p>
<p>';
 for($i=0; $i < $count; $i++)
{
if ($i == (count($select)-1)) {
$message .= $select[$i];
}
else {
$message .= $select[$i] .", ";
}
};
$message .= '</p>
<p>Kind regards</p>
<p>blah blah blah</p>
</body>
</html>';
$headers = 'From: noreply@test.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

mail($to,$subject,$message,$headers);
    }

通过这种方式,它会在复选框元素之间加上逗号,忽略最后一个逗号。

希望这有帮助。

编辑:

此外,您在每个<br />后遗漏了一些<p>个标签,不是吗?通过这种方式,您将收到带有内嵌文本的电子邮件!