1

我对 PHP 很陌生,正在尝试使用 php 在线订购网站。尝试使用“抄送”发送确认电子邮件时遇到问题。

每次处理订单时,订单总是发送到指定的“抄送”地址,但不会发送到“收件人”。很可能是由于我的代码中的错误。

在收到的电子邮件确认中,它仅显示 from 部分,而“to”部分为空,如下所示:

From: Business@business.co.uk
To: *This space is empty*
CC: orders@business.co.uk

谁能帮忙指出我哪里出错了?我附上了下面的代码。

//Code to retreive customer email
 $query  = "SELECT od_email 
 FROM tbl_order";
  $result = mysql_query($query) or die(mysql_error());
    $data = mysql_fetch_assoc($result); 

//THIS IS THE EMAIL SENT TO THE CUSTOMER and the restaurant
//define the receiver of the email
$_SESSION['od_email'] = $data['od_email'];
$sendto = $_SESSION['od_email'];
//define the subject of the email
$subject = 'Order Confirmation | Ref No for Order: '.  $_SESSION['orderId']; //this session function works properly
//define the message to be sent. Each line should be separated with \n
$message = 'test';
//Who the message is from
$from = "business@business.co.uk";
$cc = "orders@business.co.uk";
//define the headers we want passed. Note that they are separated with \r\n
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From:" . $from . "\r\n";
//bcc header going to the restaurant
$headers .= "cc:" . $cc . "\r\n";
//send the email
$mail_sent = @mail( $sendto, $subject, $message, $headers );




unset($_SESSION['od_email']);

我需要它显示的是:

From: **business@business.co.uk**
To: **$_SESSION['od_email'];**
CC: **orders@business.co.uk**

提前感谢您提供的任何帮助

4

3 回答 3

3
<?php 
session_start();
include 'connect.php';

error_reporting( error_reporting() & ~E_NOTICE);

 $message=$_POST['message'];
 $cc=$_POST['ccemail'];
 $bcc=$_POST['bccemail'];
 $emailid=$_POST['email'];
// $file=$_GET['file'];


$file=$_FILES['forwarded_file']['name'];
        $dest="admin/$file";
        $src=$_FILES['forwarded_file']['tmp_name'];

$run=mysql_query("SELECT od_email 
 FROM tbl_order");

 include 'classes/class.phpmailer.php';
 $mail = new PHPMailer(); // create a new object
 $mail->IsSMTP(); // enable SMTP
 $mail->Mailer = "smtp";
 $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
 $mail->SMTPAuth = true; // authentication enabled
 $mail->Host = "smtp.gmail.com";
 $mail->SMTPSecure = "ssl";
 $mail->Port = 465; // 587
 $mail->IsHTML(true);
 $mail->Username = "business@business.co.uk";
 $mail->Password = "******"; //Don't reveal password with others
 $mail->SetFrom("business@business.co.uk");
 $mail->Subject  = "Test";
 $mail->Body ="Test";

$mail->AddAddress($emailid);
$mail->AddCC($cc);
$mail->AddBCC($bcc);

if (isset($_FILES['$file']) &&
    $_FILES['$file']['error'] == UPLOAD_ERR_OK) {
    $mail->AddAttachment($_FILES['$file']['tmp_name'],
                         $_FILES['$file']['name']);
}

$mail->AddAttachment($_FILES['forwarded_file']['tmp_name'],
                         $_FILES['forwarded_file']['name']);
 if(!$mail->Send())
 {
  "Mailer Error: " . $mail->ErrorInfo;
  echo "<script>alert('Not Send Successfully');document.location='../abc.php'</script>";
 }
 else
 {
 echo "Message has been sent";
echo "<script>alert('Send Successfully');document.location='../abc.php'</script>";
 }

?>

这里也可以添加附件如果有的话。class.phpmailer.php是添加的库,coonect.php是连接数据库的连接文件。也可以在下面的链接查看输出

于 2016-04-05T05:46:10.250 回答
0

您正在使用会话。并且您在使用它之前还没有开始任何会话。

开始会话。将此添加到页面顶部

session_start();
于 2012-06-12T11:45:50.840 回答
0

看起来这个问题已经在评论中得到了回答 - 但完整:

$_SESSION['od_email']变量没有被填充,因此“收件人”地址为空。

解决方案是添加一个查询以从最近的订单中获取用户名:

$sql = "SELECT od_email FROM tbl_order BY od_id DESC LIMIT 1"; 
$result = mysql_query($sql) or mysql_error(); 

//define the receiver of the email 
$row = mysql_fetch_row($result); 
$to = $row[0]
于 2012-06-12T10:51:13.410 回答