-1

我创建了一个带有多维数组的表单,我正在尝试使用 PHPmailer 通过将数组的内容写入文本文件然后让它读取文件的内容来发送包含数组的电子邮件,但是当我发送 e -mail 它只显示为 "'; } fclose(); ?> "。我究竟做错了什么?

这是具有多维数组的表单的代码:

<form id="form" name="form" method="post" action="send_mail_tray.php" onsubmit='return packageValidator()'>
   <table width="100%">
      <tr>
        <td><input type="hidden" name="appetizer[]" value="Appetizer 1" />Appetizer 1</td>
        <td style="width: 75px" align="right">
            Med: <input type="text" name="appetizer[0][Med]" style="width: 30px" /><br />
            Lg: <input type="text" name="appetizer[0][Lg]" style="width: 30px" />
        </td>
        <td><input type="hidden" name="appetizer[]" value="Appetizer 2" />Appetizer 2</td>
        <td style="width: 75px" align="right">
            Med: <input type="text" name="appetizer[1][Med]" style="width: 30px" /><br />
        Lg: <input type="text" name="appetizer[1][Lg]" style="width: 30px" />
        </td>
        <td><input type="hidden" name="appetizer[]" value="Appetizer 3" />Appetizer 3</td>
        <td style="width: 75px" align="right">
            Med: <input type="text" name="appetizer[2][Med]" style="width: 30px" /><br />
            Lg: <input type="text" name="appetizer[2][Lg]" style="width: 30px" />
        </td>
      </tr>
      ...
   </table>
   <input type="Submit" value="Submit Order" />
</form>

我的PHP代码:

foreach ($_POST['appetizer'] as $piece => $sizes){
    $medSize = $sizes['Med'];
    $lgSize = $sizes['Lg'];
    if(!empty($medSize) || !empty($lgSize)){
        $appetizer .= $_GET['appetizer'] . " - Med: " . $medSize . ", Lg: " . $lgSize;
    }
}

$message = ("<p>Thank you for your catering request!</p>
    <p><b>Name:</b> $name<br />
    <b>Phone #:</b> $phone<br />
    <b>Email:</b> $email</p>
    <p><b>Appetizers:</b><br />
    $appetizer
    </p>");

我也尝试过使用 andrewsi 的代码,但使用后,我不再收到电子邮件:

foreach ($_POST['appetizer'] as $piece => $sizes){
    $medSize = $sizes['Med'];
    $lgSize = $sizes['Lg'];
    if(!empty($medSize) || !empty($lgSize)){
        $message .= $_GET['appetizer'] . " - Med: " . $medSize . ", Lg: " . $lgSize;
    }
}

下面是发送电子邮件的代码:

$email = $_REQUEST['email'];
$mail = new PHPMailer();
$mail->Host = //SMTP server
$mail->Sender = //Sender
$mail->From = //From
$mail->AddReplyTo("email@email.com");
$mail->FromName = //FromName;
$mail->AddAddress($email);
$mail->IsHTML(true);
$mail->Subject = //Subject;
$mail->Body=$message;
$mail->WordWrap = 50;
$mail->Send();
echo"<script>window.location='thankyou.html'</script>";
4

1 回答 1

2

您需要重新调整最后一段代码:

$message = "<p><b>Appetizers:</b><br />";

$fapp = fopen('appetizers.txt', 'r');
while(!feof($fapp)){
    $message .= fgets($fapp) . '<br />';
}
fclose($fapp);

您将代码与电子邮件文本混合在一起 - 这样,您就可以将它们分开。

编辑添加:

您也可以$message直接生成$_POST

foreach ($_POST['appetizer'] as $piece => $sizes){
    $medSize = $sizes['Med'];
    $lgSize = $sizes['Lg'];
    if(!empty($medSize) || !empty($lgSize)){
        $message .= $piece . " - Med: " . $medSize . ", Lg: " . $lgSize;
    }
}

这将节省您写入然后读取文件的时间。

于 2012-08-24T14:05:08.083 回答