I would like to send an array in a table as body of email. Here is my code
$mail->Subject = 'ORDER NOTICE';
$mail->Body = '<p>
Dear ' .$dn.',<br>
An Order has been made on '. $date .'to be delivered to this address; '.$da.'<br>
Please expect your order within 2 days.
Please note that this order is Pay on delivery.<br>
These are the Items to be delivered '.implode("<br>", $_SESSION["cart"]);
</p>';
The email sends but the array stored in the $_SESSION["cart"] is not displayed
It seems likely to me that what you actually want will involve parsing the content of the variable, and not just blindly dumping its contents, so i'll answer both.
The body of the e-mail is html, so this is really no different than displaying the array in the browser. You cannot simply echo its contents though (like with implode
). You have to encode it as a text so that it doesn't corrupt the entire document/email; What if some string in that array contained the string 'but Baseball < Football !'. That <
would be seen as the start of an HTML element, and break everything.
Luckily PHP has a build-in function for this, var_export
; we can use that together with the nl2br
function which adds <br>
s on newlines.
So you could replace implode("<br>", $_SESSION["cart"])
with nl2br(var_export($_SESSION["cart"], true))
What you more likely should do though, is just treat the html e-mail as another document (just like your view my cart
page which your website likely has).
$html = '';
$html .= sprintf('<p>Dear <em>%s</em>,',
htmlentities($dn) );
$html .= sprintf('An Order has been made on <strong>%s</strong>',
htmlentities($date) );
$html .= sprintf('<p>to be delivered to this address:</p>');
$html .= sprintf('<pre>%s</pre><br>',
htmlentities($da) );
$html .= sprintf('<p>Please expect your order within <strong>%s</strong></p>',
'2 days' );
$html .= sprintf('<p>Please note that this order is Pay on delivery.</p>');
$html .= sprintf('<p>These are the Items to be delivered:</p>');
$html .= sprintf('<ul>');
foreach ($_SESSION["cart"] as $line) {
// Do something with each entry here (i dont know whats in there)
$html .= sprintf('<li>%s x %s</li>', // maybe something like this:
'5x', // - amount
'Blue T-shirt' ); // - description
}
$html .= sprintf('</ul>');
$html .= sprintf('<p>See you soon !</p>');
$mail->Subject = 'ORDER NOTICE';
$mail->Body = $html;