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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)