0

我想要做的是从一个站点生成一个购物车,然后通过电子邮件发送它。当我运行我的代码时,该功能在页面上运行并且不通过电子邮件发送,但它显示正确。我无法将函数的输出存储到可以在电子邮件中使用的变量中。这是我的代码...

function emailcartview() {
$total=0;
$totalvalue=0;
foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_'){
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, name, cost, item_number FROM mydbname WHERE id='.mysql_real_escape_string((int)$id));


            $cart = array();
            while ($get_row = mysql_fetch_assoc($get)) {
                $cart[] = array(
                'hsid' => $get_row['id'],
                'cost' => $get_row['cost'],
            $cost = $get_row['cost'],
                'itemno' => $get_row['item_number'],
                'name' => $get_row['name'],
                'value' => $value,
                $sub = $value*$cost,
                $total += $sub,
                $totalvalue = $value,
                'realsub' => number_format($sub, 2),
                );

                }
                foreach($cart as $index => $record){
                    global $emailcart;
                    $emailcart = "Item: {$record['itemno']} - {$record['name']} | Qty: {$record['value']} | Cost: {$record['cost']} x {$record['value']} = {$record['realsub']} <br>";
                    echo $emailcart;


            }

        }

    }

}
echo "Total : $".number_format($total, 2);
 }


$emailSubject = 'Invoice Submission';
$webMaster = 'my_email@blahblah.com';


$companyName = $_POST['companyname'];
$contactName = $_POST['contactname'];
$contactEmail = $_POST['contactemail'];
$teleNumber = $_POST['telenumber'];
$billAddr = $_POST['billaddr'];
$shipAddr = $_POST['shipaddr'];
$neededBy = $_POST['neededby'];
$cartprint .= emailcartview();

$body = "
<br><hr><br>
Company Name: $companyName <br>
Contact Name: $contactName <br>
Contact Email: $contactEmail <br>
Telephone Number: $teleNumber <br>
Billing Address: $billAddr <br>
Shipping Address: $shipAddr <br>
Date Needed: $neededBy <br> <br>
<br><hr><br>
$cartprint";

$headers = "From: ordersubmission@blahblah.com\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

 ?>

我尝试过但没有奏效的奇怪东西可能有很多残余物,而不是最干净的。任何帮助将不胜感激!!

泰勒。

4

1 回答 1

1

在函数 emailcartview 中,您需要生成字符串而不是 echo 语句,并且在函数结束时您应该返回该字符串。

检查以下代码:

function emailcartview() {
$total=0;
$totalvalue=0;
$str = '';
foreach($_SESSION as $name => $value) {
    if ($value>0){
        if (substr($name, 0, 5)=='cart_'){
            $id = substr($name, 5, (strlen($name)-5));
            $get = mysql_query('SELECT id, name, cost, item_number FROM mydbname WHERE id='.mysql_real_escape_string((int)$id));


            $cart = array();
            while ($get_row = mysql_fetch_assoc($get)) {
                $cart[] = array(
                'hsid' => $get_row['id'],
                'cost' => $get_row['cost'],
            $cost = $get_row['cost'],
                'itemno' => $get_row['item_number'],
                'name' => $get_row['name'],
                'value' => $value,
                $sub = $value*$cost,
                $total += $sub,
                $totalvalue = $value,
                'realsub' => number_format($sub, 2),
                );

                }
                foreach($cart as $index => $record){
                    global $emailcart;
                    $str. = "Item: {$record['itemno']} - {$record['name']} | Qty: {$record['value']} | Cost: {$record['cost']} x {$record['value']} = {$record['realsub']} <br>";

            }

        }

    }


}
$str. = "Total : $".number_format($total, 2);
return $str;
 }


$emailSubject = 'Invoice Submission';
$webMaster = 'my_email@blahblah.com';


$companyName = $_POST['companyname'];
$contactName = $_POST['contactname'];
$contactEmail = $_POST['contactemail'];
$teleNumber = $_POST['telenumber'];
$billAddr = $_POST['billaddr'];
$shipAddr = $_POST['shipaddr'];
$neededBy = $_POST['neededby'];
$cartprint .= emailcartview();

$body = "
<br><hr><br>
Company Name: $companyName <br>
Contact Name: $contactName <br>
Contact Email: $contactEmail <br>
Telephone Number: $teleNumber <br>
Billing Address: $billAddr <br>
Shipping Address: $shipAddr <br>
Date Needed: $neededBy <br> <br>
<br><hr><br>
".emailcartview();

$headers = "From: ordersubmission@blahblah.com\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

 ?>
于 2012-11-20T04:38:19.923 回答