Skip to content
Advertisement

Unable to send SESSION DATA to email via PHPmailer

I am trying to send an email upon successful order placed on my website, however the email delivers but the details (WHICH ARE STORED IN THE SESSION VARIABLE $_SESSION[“cart”]) doesn’t send along with it.

Here is my php code

require(ROOT_PATH . 'phpmailer/src/Exception.php'); 
require(ROOT_PATH . 'phpmailer/src/PHPMailer.php'); 
require(ROOT_PATH . 'phpmailer/src/SMTP.php');
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER;                     
// Enable verbose debug output
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Hostname = '197.210.53.47';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = "browynlouis2@gmail.com";
$mail->Password = '081';
//Recipients
$mail->setFrom('browynlouis2@gmail.com', 'Sativa Merchandise');
$mail->addAddress($_SESSION["order_details"]["email"]);               // Name is optional
$mail->addReplyTo('browynlouis2@gmail.com', 'Sativa Merchandise');
// Content
$mail->isHTML(true);                                 
// Set email format to HTML

$body = '<html lang="en"><head></head><body>';

    $body .= '<section class="shop checkout section" style="text-align:center; padding:20px; background:white"><div class="container"><div class="inner-top"><div class="row"><div><div class="inner">';
    
    $body .= '<a href="localhost:8080/phpmyadmin/online_store/index.php"><img src="cid:logo" width="40" height="50"></a><br><br><h1>ORDER CONFIRMATION</h1/><p style="font-weight: lighter;color: grey;font-size:18px;line-height:28px"> You are getting this email in regards to your successful order on www.sativamerch.com with order ID <b>'. $_SESSION["order_details"]["order_id"] .'</b>. You can view your <span style="color:#0088CC">order details</span> below.</p>';

    
     $body .= '<div style="text-align:center;width:100%;"><?php foreach($_SESSION["cart"] as &$list) { ?> ';

    
    $mail->AddEmbeddedImage('admin/assets/img/products/'. $list["image"] . '', ''. $list["image"] . '');


     $body .= '<div class="product" style="margin:1%;"><img src="cid:'. $list["image"] . '" width="100px" height="120px" style="border:none"><div class="product-content"><h3>'. $list["name"] .'</h3><span style="font-weight: lighter;font-size:15px;line-height:28px">'. $list["price"] .' </span><br><span style="font-weight: lighter;font-size:15px;line-height:28px">'. $list["quantity"] .' X '. $list["price"] .' = '. $list["quantity"] * $list["price"] .' </span><br></div>';

   $body .= '<?php } ?></div>';

   $body .= '<br><br><div class="details"><h3>SHIPPING DETAILS</h3><p style="font-size:18px"><b>NAME</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>EMAIL</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>PHONE</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>CITY</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>STATE</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>ADDRESS Line 1</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>ADDRESS Line 2</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>NIGERIA</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>ZIP</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br><p style="font-size:18px"><b>COMPANY</b></p><span style="font-weight: lighter;font-size:15px;">N8,000 </span><br></div></div>';

    
    $body .= '</div></div></div></div></div></section></body></html>';
    
    $mail->Subject = 'ORDER CONFIRMATION : Your order with ID '. $_SESSION["order_details"]["order_id"] .' has been placed';
    $mail->AddEmbeddedImage('admin/assets/img/logo.jpg', 'logo');
    $mail->Body    = $body;
    $mail->AltBody = nl2br(strip_tags($body));
    if (!$mail->send()) {
    $status = "YOUR ORDER COULDN'T BE PLACED";
    } else {
        // Rest code

    } 

And here is an image of the email to display what I am talking about better

The Email which sends upon processing

Advertisement

Answer

You’re building your content incorrectly. This will not work:

$body .= '<div style="text-align:center;width:100%;"><?php foreach($_SESSION["cart"] as &$list) { ?> ';

The PHP tags inside the single quotes will be ignored, which should be apparent if you inspect the source of a received message. Go back into PHP mode to run that code, like this:

$body .= '<div style="text-align:center;width:100%;">';
foreach($_SESSION["cart"] as &$list) {
    $body .= //stuff from the session added here...
}

You could use your original code if eval() what ends up in $body before using it, but that would be slower and somewhat dangerous.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement