Click here to Skip to main content
15,896,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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(); // set the resulting array to associative $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 ""; ?>

Source:
PHP
<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();

    // set the resulting array to associative
    $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;
  /*Make a loop that will continue until
  no switching has been done:*/
  while (switching) {
    //start by saying: no switching is done:
    switching = false;
    rows = table.rows;
    /*Loop through all table rows (except the
    first, which contains table headers):*/
    for (i = 1; i < (rows.length - 1); i++) {
      //start by saying there should be no switching:
      shouldSwitch = false;
      /*Get the two elements you want to compare,
      one from current row and one from the next:*/
      x = rows[i].getElementsByTagName("TD")[0];
      y = rows[i + 1].getElementsByTagName("TD")[0];
      //check if the two rows should switch place:
      if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
        //if so, mark as a switch and break the loop:
        shouldSwitch = true;
        break;
      }
    }
    if (shouldSwitch) {
      /*If a switch has been marked, make the switch
      and mark that a switch has been done:*/
      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:

PHP
$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.
Posted
Updated 17-May-20 14:03pm
v3

1 solution

I hate to break it to you but you cannot run any code in an HTML email. It would be a huge security risk.
 
Share this answer
 
v2
Comments
Cody O'Meara 17-May-20 21:04pm    
Do you know of another solution to sort of getting to what I'm trying to do? Its only data so maybe it can read it from another file instead of querying it directly?

I would think there would be a way, how do businesses send emails with buttons, personal information, and what not?

Thanks for the reply!
Dave Kreskowiak 17-May-20 21:23pm    
The only way to do it is to build the table on the server-side before the email is sent.

Buttons are standard HTML, so no code needs to run there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900