在MSG标记内插入php代码

I have this code which sends an e-mail with some information in it. The message part is like below:

    $mail->Body    =<<<MSG
            <html>
                <head>
                    <title>Statistics</title>
                </head>
                <body>
                    The start was: {$start} </br></br>
                    The end was: {$end} </br></br>            
                </body>
            </html>
    MSG;

I have an array named $events which I want to include in the message, but I want to display it as a table, as below

$mail->Body    =<<<MSG
        <html>
            <head>
                <title>Statistics</title>
            </head>
            <body>
                The start was: {$start} </br></br>
                The end was: {$end} </br></br>   

                foreach ($events as $event)
                { 
                    echo '<tr>';
                    echo '<td>' . $event)[1] . '</td>';
                    echo '<td>' . $event)[2] . '</td>';
                    echo '<td>' . $event)[3] . '</td>';
                    echo '</tr>';
                }

            </body>
        </html>
MSG;

Can you please help me with the syntax of foreach?

You want to display events as a table in an email. Well your foreach in itself is correct but what you do inside has some problems. The most simple solution to your question would be as follows (I never used the technique you are using... Im quite sure that the foreach can not be executed this way):

$mail->Body    =<<<MSG
    <html>
        <head>
            <title>Statistics</title>
        </head>
        <body>
            The start was: {$start} </br></br>
            The end was: {$end} </br></br>   
            <table>
            foreach ($events as $event)
            { 
                echo '<tr>';
                echo '<td>' . $event . '</td>';
                echo '</tr>';
            }
            </table>

        </body>
    </html>
MSG;

As stated in comments it would be better to use normal strings:

$body = '
<html>
    <head>
        <title>Statistics</title>
    </head>
    <body>
        The start was: '.$start.' </br></br>
        The end was: '.$end.' </br></br>   
        <table>';
        foreach ($events as $event)
        { 
            $body .= '<tr><td>' . $event . '</td></tr>';
        }
        $body .= '</table>
    </body>
</html>';
$mail->Body = $body;

I assumed your code segment echo '<td>' . $event)[1] . '</td>'; echo '<td>' . $event)[2] . '</td>'; echo '<td>' . $event)[3] . '</td>'; to be your first try to handle an array. if $eventsis multi-dimensional (array of array) you would have to replace

$body .= '<tr><td>' . $event . '</td></tr>';

with

$body .= '<tr><td>' . $event[1] . '</td><td>' . $event[1] . '</td><td>' . $event[1] . '</td></tr>';

remember that array indices start with 0

Prepare your data before heredocs:

$tableData = '';

foreach ($events as $event) { 
    $tableData .= '<tr>';

    foreach ($event as $cell) {
        $tableData .= "<td>{$cell}</td>";
    }

    $tableData .= '</tr>';
}

$mail->Body = <<<MSG
        <html>
            <head>
                <title>Statistics</title>
            </head>
            <body>
                The start was: {$start} </br></br>
                The end was: {$end} </br></br>

                <table>{$tableData}</table>
            </body>
        </html>
MSG;