Search code examples
phphtmlphpmailer

Running executable code within PHPMailer


Please see the code that I have within the BODY of PHPMailer. System reports a Syntax error on the line $mail->Body .= ''. $row['updated_by'] . ''; I have read through documentation of PHPMailer but have not been able to find much about inserting PHP within the BODY part of the email.

$mail->Body = <<

        <table class="table table-striped table-bordered sortable">
              <thead>
                <tr>
                  <th>Sales Agent</th>
                  <th>Company</th>
                  <th>Contact</th>
                  <th>Contact Medium</th>
                  <th>Contact Date</th>
                  <th>Result</th>
                  </tr>
              </thead>
              <tbody>

              <?php 
                include 'database.php';
                $pdo = Database::connect();

                $yesterday =  date('Y-m-d 00:00:00',strtotime("-1 days"));
                $sql = "SELECT * FROM `customer_history` WHERE `date_contacted` = '$yesterday'";
                $sth = $pdo->prepare($sql);
                $sth->execute();

                foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) 

                {
                        $mail->Body .= '<tr>';
                        $mail->Body .= '<td>'. $row['updated_by'] . '</td>';
                        $mail->Body .= '<td>'. $row['company_name'] . '</td>';
                        $mail->Body .= '<td>'. $row['first_name'] . '</td>';
                        $mail->Body .= '<td>'. $row['contacted_by'] . '</td>';
                        $mail->Body .= '<td>'. $row['date_contacted'] . '</td>';
                        $mail->Body .= '<td>'. $row['last_result'] . '</td>';
                        $mail->Body .= '</td>';
                        $mail->Body .= '</tr>';
                }
               Database::disconnect();
              ?>
</tbody>
</table>
</div>
</div>

END;


Solution

  • Firstly execute db query and after that add result to the body.

    <?php 
    
    $history = '';
    
    include 'database.php';
    $pdo = Database::connect();
    
    $yesterday =  date('Y-m-d 00:00:00',strtotime("-1 days"));
    $sql = "SELECT * FROM `customer_history` WHERE `date_contacted` = '$yesterday'";
    $sth = $pdo->prepare($sql);
    $sth->execute();
    
    foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $row) 
    {
        $history .= '<tr>';
        $history .= '<td>'. $row['updated_by'] . '</td>';
        $history .= '<td>'. $row['company_name'] . '</td>';
        $history .= '<td>'. $row['first_name'] . '</td>';
        $history .= '<td>'. $row['contacted_by'] . '</td>';
        $history .= '<td>'. $row['date_contacted'] . '</td>';
        $history .= '<td>'. $row['last_result'] . '</td>';
        $history .= '</td>';
        $history .= '</tr>';
    }
    Database::disconnect();
    
    // Mail body
    $mail->Body = <<<EOF
    ...
        <tbody>
            {$history}
        </tbody>
    ...
    EOF;