duanao4503 2019-05-22 19:51
浏览 47
已采纳

PHP MAIL:我的一个变量发送“false”值

I am sending an e-mail with different variables as an JSON encoded array from an online store. I get the mail just fine and all the data is in there except for one of the variables, which is a JSON encoded array by itself. this particular variable shows as "false" in the e-mail. I'm missing something?

I'm using PHP mail to do it.

<?php
require_once "Mail.php";

$link = mysqli_connect("localhost", "xxx", "xxx", "xxx");

if ($link === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql = "SELECT * FROM users WHERE user = '" . $_SESSION['logged'] . "'";
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);

$user = $_SESSION['logged'];
$rua = $_POST['rua'];
$numero = $_POST['numero'];
$apt = $_POST['apt'];
$cep = $_POST['cep'];
$total = $_POST['total'];
$comment = $_POST['observacion'];
$from = $row['mail'];
$mailTo = "xxxxx@hotmail.com";
$subject = "compra online - no cep";

$compra = $_SESSION["shopping_cart"];
$compra2 = json_encode($compra);

$bod = array(
    'user' => $user,
    'rua' => $rua,
    'cep' => $cep,
    'compra' => $compra2,
    'comment' => $comment,
    'total' => $total,
);

$body = json_encode($bod);

$headers = array(
    'From' => $from,
    'To' => $mailTo,
    'Subject' => $subject,
);

$smtp = Mail::factory('smtp', array(
    'host' => 'smtp-mail.outlook.com',
    'port' => '587',
    'auth' => true,
    'username' => 'xxxxxxx@hotmail.com',
    'password' => 'xxxxxxx',
));

$mail = $smtp->send($mailTo, $headers, $body);

if (PEAR::isError($mail)) {
    echo ("<p>" . $mail->getMessage() . "</p>");
} else {
    header("Location: lojacart.php?mailok");
}

mysqli_close($link);

I'm getting the e-mail with all the data except for the $compra2 variable which is showing as 'compra'=false. now if I echo the variable $compra2 it actually have a large string of data on it.

I think it might have to do with the variable being a JSON encoded session, but I'm not sure.

  • 写回答

1条回答 默认 最新

  • dpp89959 2019-05-23 00:16
    关注

    After a lot of trying and getting frustrated I came with a solution, for those looking for a something like this:

    I added a hidden input to the form on the previous page and assigned it a value as "print_r($SESSION['whatever you session is called'], TRUE)" ...

    on the next page I took the $_POST[] form that input and added the variable to the $body array for the mail().

    It will print some array garbage in the middle BUT it works like a charm! and I cleaned all the "extra" text form the array using preg_replace(). It may not be an elegant way of doing it but as I said... it worked

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题