从数据库将动态值加载到phpmailer主体的msghtml中

7d7tgy0s  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(531)

我在这里尝试的是通过smtp phpmailer插入和发送订单数。我使用foreach将订单数据分离为键值对并插入数据库。
您可以在这里看到:

一旦执行成功,现在我想发送一封邮件给业主与订单的细节,这是刚刚插入。只是为了测试,我只选择productid作为订单细节。
这是刚到的邮件。

问题
我看到的是数据库已经插入了2个productid(26,27),但是在邮件中我只得到了productid(26),我希望在邮件体中插入动态productid。我使用while循环,但我认为目标无法实现。如果有人打印出我的错误,我将不胜感激。
PHP:

  1. $shown = false;
  2. foreach($_SESSION["shopping_cart"] as $number => $val)
  3. {
  4. // prepare and bind
  5. $stmt = $conn->prepare("INSERT INTO purchase(guest_code,productid,quantity,date_purchase) VALUES (?, ?, ?, ?)");
  6. $stmt->bind_param("siis",$_SESSION['CODE'],$val['product_id'],$val['product_quantity'],$current_date_time); //inserting mutiple value in db successful.
  7. if($stmt->execute())
  8. {
  9. if(!$shown) //show msg or dedirect only once
  10. {
  11. $sql= mysqli_query($conn,"select productid from purchase where guest_code='".$_SESSION['CODE']."'"); //fetch only productid for test
  12. while ($row = mysqli_fetch_array($sql))
  13. {
  14. $productid = $row['productid'];
  15. $message = "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
  16. $mail->msgHTML($message); //trying to load multiple productid in $message
  17. }
  18. $mail->Subject = 'New Order Arrived!';
  19. if(!$mail->send())
  20. {
  21. $mail_error = 'error: ' . $mail->ErrorInfo;
  22. exit();
  23. }
  24. else {
  25. //if mail sent then
  26. unset($_SESSION["shopping_cart"]); //distroy all the values in the cart
  27. //header('location:../checkout.php?er=false');
  28. }
  29. $shown = true;
  30. }
  31. }else
  32. {
  33. if(!$shown) //show msg only once
  34. {
  35. echo 'ERROR: while placing your order. Please contact restaurant owner.';
  36. $shown = true;
  37. }
  38. }
  39. }
twh00eeo

twh00eeo1#

每次你打电话 $mail->msgHTML($message); 它将替换 Body ; 这并没有增加它。我希望你能按照以下思路来做:

  1. $mail->Body = '<p>Your order</p><table>';
  2. while ($row = mysqli_fetch_array($sql)) {
  3. $productid = $row['productid'];
  4. $mail->Body .= "<tr><td style='text-align:center;'><strong>".$productid."</td></tr>";
  5. }
  6. $mail->Body .= '</table>';
pepwfjgg

pepwfjgg2#

我找到的解决方案。
所以,我以前遇到的问题是 sendmail() 内部函数 foreach() 最重要的是,只显示一次消息,我使用if条件来阻止 $row['productid'] 只显示一个productid,结果用productid恢复邮件。
相反,我必须这样做:

  1. if(!empty($_SESSION["shopping_cart"]) && $_SESSION["shopping_cart"]!=='')
  2. {
  3. $shown = 0;
  4. foreach($_SESSION["shopping_cart"] as $number => $val)
  5. {
  6. // prepare and bind
  7. $stmt = $conn->prepare("INSERT INTO purchase(guest_code,productid,quantity,date_purchase) VALUES (?, ?, ?, ?)");
  8. $stmt->bind_param("siis",$_SESSION['CODE'],$val['product_id'],$val['product_quantity'],$current_date_time); //inserting mutiple value in db successful.
  9. if($stmt->execute())
  10. {
  11. $body .= '<p><b>Product ID:</b>&nbsp;&nbsp;&nbsp;'.$val['product_id'].'</p>';
  12. $body .= '<p><b>Discussion Name:</b>&nbsp;&nbsp;&nbsp;'.$val['product_quantity'].'</p>';
  13. echo 'success';
  14. }else
  15. {
  16. if($shown==0) //show msg only once
  17. {
  18. echo 'ERROR: while placing your order. Please contact restaurant owner.';
  19. header('location:../checkout.php?er=true');
  20. $shown = 1;
  21. }
  22. }
  23. }
  24. $mail->msgHTML($body);
  25. $mail->Subject = 'New Order Arrived!';
  26. $mail->send();
  27. }

邮件输出:

展开查看全部

相关问题