user2968767
user2968767

Reputation:

php form being sent to email not generating proper output?

I have the following. Im trying to send a table of data through to an email address but this code is not working I get the following error:

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

Here is the processing code:

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

 $to = '[email protected]'; 
 $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"; 
    } 
?> 

Could someone please advise? I really need to get this working today. Please please someone help.

Upvotes: 0

Views: 90

Answers (3)

Roopendra
Roopendra

Reputation: 7776

Try this:- you are using foreach inside echo

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

$to = '[email protected]';
$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";
}
?> 

Upvotes: 0

Vlad Cazacu
Vlad Cazacu

Reputation: 1540

You are using <?php ?> tags in your $body string. You should move the code from between those tags outside the string and concatenate.

Upvotes: 0

q0re
q0re

Reputation: 1359

Do this:

    $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>
";

Upvotes: 1

Related Questions