无法通过 PHPmailer 将会话数据发送到电子邮件

Unable to send SESSION DATA to email via PHPmailer

我正在尝试在我的网站上成功下单后发送电子邮件,但是电子邮件已送达,但详细信息(存储在会话变量 $_SESSION["cart"] 中)并未随邮件一起发送.

这是我的 php 代码

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

    } 

这是电子邮件的图片,可以更好地显示我在说什么

您构建的内容不正确。这行不通:

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

单引号内的 PHP 标签将被忽略,如果您检查收到消息的来源,这应该很明显。返回 PHP 模式以 运行 该代码,如下所示:

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

可以 使用你的原始代码,如果 eval() 在使用它之前 $body 结束,但那样会更慢并且有点危险。