【问题标题】:for loop inside phpmailer message?phpmailer消息中的for循环?
【发布时间】:2013-09-09 16:33:42
【问题描述】:

我有一个表格,每行都显示有复选框,用户可以选择多个复选框然后单击提交,然后这将使用 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);
    }
    }

感谢您的帮助!

【问题讨论】:

    标签: php html phpmailer


    【解决方案1】:

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

    请向我们提供您的 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);
        }
    

    通过这种方式,它会将复选框元素与它们之间的逗号忽略最后一个逗号。

    希望这会有所帮助。

    编辑:

    另外,您在每个&lt;p&gt; 之后缺少一些&lt;br /&gt; 标签,不是吗?这样您就会收到带有内嵌文字的电子邮件!

    【讨论】:

      猜你喜欢
      • 2012-12-08
      • 1970-01-01
      • 2017-10-13
      • 2011-04-15
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多