PHP 循环邮件程序

PHP Mailer for loop

我是 PHP 的新人。目前,我正在写一个 PHP‖mailer 来发送订单确认邮件。有一个交易清单。我想把它放在 table 中并附加到 php 邮件正文中。该列表来自我的后端服务,格式为 json。我收到 解析错误:语法错误,意外标记“for”。

Json:

{
    "email":"test@gmail.com",
    "address":"Barcelona",
    "address_type":"Office",
    "amount":12,
    "mobile":"7152820391",
    "name":"Boris Caria",
    "order_number":"20bs10d0ea15",
    "transaction_list":[
        {
            "transaction_id":"0012331213213",
            "transaction_amount":12,
            "transaction_time":"12:36:02"
        },
        {
            "transaction_id":"0012312132235",
            "transaction_amount":24.5,
            "transaction_time":"12:38:23"
        }
    ]
}

php:

$data = json_decode(file_get_contents('php://input'), true);
$mail->Body    = '<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet" type="text/css">
    <div class="email">
        <div style="font-size:16px;">
            <p>Dear Customer,</p>
            <br>
            <p>Thanks for shopping at <b style="color:#64b3f4;">E shop</b>!</p>
            <p>Order Number: <b>'. $data["order_number"].'</b></p>
            <p>Date: '. date('jS\, F Y h:i:s A'). '</p>
            <br>
            <p>Your order is below:</p>
            <table border="0">
              
              <tr>
                <td scope="col">Women\'s Light Blue Tunic</td>
                <td scope="col"> * 1</td>
                <td scope="col"> &nbsp; </td>
                <td scope="col">.00</td>
              </tr>
              <tr>
                <td scope="col"><b>Total</b></td>
                <td scope="col">&nbsp;</td>
                <td scope="col"><b> &nbsp; </b></td>
                <td scope="col"><b>$'. $data["amount"] .'</b></td>
              </tr>
            </table>        
            <table border="0">
              <tr>
                <td scope="col">Transaction ID</td>
                <td scope="col">Transaction amount)</td>
                <td scope="col">Transaction time</td>
              </tr>'.
               for($x = 0; $x < sizeof($data["transaction_list"]); $x++){
                <tr>
                <td scope="col">$data["transaction_list"][x][transaction_id]</td>
                <td scope="col">$data["transaction_list"][x][transaction_amount]</td>
                <td scope="col"> $data["transaction_list"][x][transaction_time].</td>
                </tr>
                }.'
            </table>
            <p>Shipping information is below:</p>
            <table border="0">
              <tr>
                <td scope="col">Full Name: </td>
                <td scope="col">'. $data["name"] .'</td>
              </tr>
              <tr>
                <td scope="col">Mobile number: </td>
                <td scope="col">'. $data["mobile"] .'</td>
              </tr><tr>
                <td scope="col">Address: </td>
                <td scope="col">'. $data["address"] .'</td>
              </tr><tr>
                <td scope="col">Address Type: </td>
                <td scope="col">'. $data["address_type"] .'</td>
              </tr><tr>
                <td scope="col">Email: </td>
                <td scope="col">'. $data["email"] .'</td>
              </tr>
            </table>
            <p>Thank you for your order.</p>
        </div>
    </div>';
     $data = json_decode(file_get_contents('php://input'), true);            
     $top    = '<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,600" rel="stylesheet" type="text/css">
    <div class="email">
        <div style="font-size:16px;">
            <p>Dear Customer,</p>
            <br>
            <p>Thanks for shopping at <b style="color:#64b3f4;">E shop</b>!</p>
            <p>Order Number: <b>'. $data["order_number"].'</b></p>
            <p>Date: '. date('jS\, F Y h:i:s A'). '</p>
            <br>
            <p>Your order is below:</p>
            <table border="0">
              
              <tr>
                <td scope="col">Women\'s Light Blue Tunic</td>
                <td scope="col"> * 1</td>
                <td scope="col"> &nbsp; </td>
                <td scope="col">.00</td>
              </tr>
              <tr>
                <td scope="col"><b>Total</b></td>
                <td scope="col">&nbsp;</td>
                <td scope="col"><b> &nbsp; </b></td>
                <td scope="col"><b>$'. $data["amount"] .'</b></td>
              </tr>
            </table>        
            <table border="0">
              <tr>
                <td scope="col">Transaction ID</td>
                <td scope="col">Transaction amount)</td>
                <td scope="col">Transaction time</td>
              </tr>';
              foreach ($data["transaction_list"] as $key => $value) {
                $middle[$key] = 
                '<tr><td scope="col">'.$value["transaction_id"].'</td><td scope="col">'.$value['transaction_amount'].'</td><td scope="col">'.$value['transaction_time'].'</td></tr>';
            }
            $bottom = '</table>
            <p>Shipping information is below:</p>
            <table border="0">
              <tr>
                <td scope="col">Full Name: </td>
                <td scope="col">'. $data["name"] .'</td>
              </tr>
              <tr>
                <td scope="col">Mobile number: </td>
                <td scope="col">'. $data["mobile"] .'</td>
              </tr><tr>
                <td scope="col">Address: </td>
                <td scope="col">'. $data["address"] .'</td>
              </tr><tr>
                <td scope="col">Address Type: </td>
                <td scope="col">'. $data["address_type"] .'</td>
              </tr><tr>
                <td scope="col">Email: </td>
                <td scope="col">'. $data["email"] .'</td>
              </tr>
            </table>
            <p>Thank you for your order.</p>
        </div>
    </div>';
    $mail->Body = $top.implode('', $middle).$bottom;

Try This maybe that helps you