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

    评论
    解决 无用
    打赏 举报