I am trying to send an email with PHPMailer. When the email sends it doesn't display the table but part of the code.
This is how the email comes in as text
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT fullName, result FROM results where dateTime > CURDATE() "); $stmt->execute(); ?>
Source:
<pre> $mail->isHTML(true);
$mail->Body = '
<html>
<style>
table {
border-spacing: 0;
border: 1px solid #ddd;
}
th, td {
text-align: left;
padding: 16px;
}
tr:nth-child(even) {
background-color: #f2f2f2
}
</style>
<body onload="sortTable()">
<center>
<?php
echo "<table id=\'myTable\' style=\'border: solid 1px black;\'>";
echo "<tr><th>FullName</th><th>result</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style=\'width: 150px; border: 1px solid black;\'>\' . parent::current(). \'</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "REMOVED";
$password = "REMOVED";
$dbname = "REMOVED";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT fullName, result FROM results where dateTime > CURDATE() ");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>
</center>
<script>
function sortTable() {
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[0];
y = rows[i + 1].getElementsByTagName("TD")[0];
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
</script>
</body>
</html>';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) { // handle error.
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
What I have tried:
To troubleshoot I tried taking anything out with "->" and that seems to be what the cause is for example I underlined them:
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT fullName, result FROM results where dateTime > CURDATE() ");
$stmt->execute();
I could be completely wrong but not sure at this point.
Also to note, all that is in the mail body, works when displayed on a regular page.