PHP表单被发送到电子邮件不生成正确的输出?

时间:2013-12-19 10:39:47

标签: php forms email

我有以下内容。我试图将数据表发送到电子邮件地址,但此代码无法正常工作我收到以下错误:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

以下是处理代码:

<?php 
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "From: Sales Rep <darrenascas@casc.com>\r\n";
$subject = 'Email report';

 $to = 'domscinic.masccafee@emaccasre.com'; 
 $subject = "Web Contact Data"; 




$body = "

<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table border='1'>
    <thead>
        <tr>
<?php

     if (!empty($_POST)) {

    echo '<tbody>';
    foreach($_POST['itemCode'] as $row => $item) {
     echo'<tr>';
     echo '<td>' . htmlspecialchars($_POST['itemCode'][$row]) . "" . "</td>";
     echo '<td>' . htmlspecialchars($_POST['itemDesc'][$row]) . "" . "</td>";
     echo '<td>' . htmlspecialchars($_POST['itemQty'][$row]) . "" . "</td>";
     echo '<td>' . '&#8364;' . htmlspecialchars($_POST['itemPrice'][$row]) . "" . "</td>    ";
     echo '<td>' . '&#8364;' . htmlspecialchars($_POST['itemLineTotal'][$row]) . "" . "</td>";
    echo'<tr>';


}
echo '</tbody>';

        }

    ?>
        </tr>
    </thead>
</table>
</body>
</html>
";




 $send = mail($to, $subject, $body, $headers); 
 if($send){
   // header( "Location:index.php" );
    } else {
        print "We encountered an error sending your mail, please try again"; 
    } 
?> 

有人可以提供建议吗?我真的需要今天开始工作。请有人帮忙。

3 个答案:

答案 0 :(得分:1)

这样做:

    $body = "

<html>
<head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table border='1'>
    <thead>
        <tr>";

     if (!empty($_POST)) {

    $body .= '<tbody>';
    foreach($_POST['itemCode'] as $row => $item) {
     $body .= '<tr>';
     $body .= '<td>' . htmlspecialchars($_POST['itemCode'][$row]) . "" . "</td>";
     $body .= '<td>' . htmlspecialchars($_POST['itemDesc'][$row]) . "" . "</td>";
     $body .= '<td>' . htmlspecialchars($_POST['itemQty'][$row]) . "" . "</td>";
     $body .= '<td>' . '&#8364;' . htmlspecialchars($_POST['itemPrice'][$row]) . "" . "</td>    ";
     $body .= '<td>' . '&#8364;' . htmlspecialchars($_POST['itemLineTotal'][$row]) . "" . "</td>";
    $body .='<tr>';


}
$body .= '</tbody>';

        }

$body .= "</tr>
    </thead>
</table>
</body>
</html>
";

答案 1 :(得分:0)

您在$ body字符串中使用<?php ?>标记。您应该从字符串之外的那些标记之间移动代码并连接。

答案 2 :(得分:0)

试试这个: - 你在echo中使用foreach

<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "From: Sales Rep <darrenascas@casc.com>\r\n";
$subject = 'Email report';

$to = 'domscinic.masccafee@emaccasre.com';
$subject = "Web Contact Data";

$body = "<html><head>
<title>HTML email</title>
</head>
<body>
<p>This email contains HTML Tags!</p>
<table border='1'>
    <thead>
        <tr>";
if (!empty($_POST)) {
    $body .= "<tbody>";
    foreach ($_POST['itemCode'] as $row => $item) {
        $body .= '<tr > ';
        $body .= '<td>' . htmlspecialchars($_POST['itemCode'][$row]) . "" . "</td>";
        $body .= '<td>' . htmlspecialchars($_POST['itemDesc'][$row]) . "" . "</td>";
        $body .= '<td>' . htmlspecialchars($_POST['itemQty'][$row]) . "" . "</td>";
        $body .= '<td>' . '&#8364;' . htmlspecialchars($_POST['itemPrice'][$row]) . "" . "</td>    ";
        $body .= '<td>' . '&#8364;' . htmlspecialchars($_POST['itemLineTotal'][$row]) . "" . "</td>";
        $body .= '<tr > ';
    }
    $body .= '</tbody>';
}
$body .= '</tr></thead></table></body></html>';

$send = mail($to, $subject, $body, $headers);
if ($send) {
    // header( "Location:index.php" );
} else {
    print "We encountered an error sending your mail, please try again";
}
?>