2012-06-12 73 views
1

我對PHP相當陌生,正在用php在線訂購網站。使用'cc'發送確認電子郵件時遇到問題。在php中發送電子郵件?

每次處理訂單時,訂單總是發送到指定的'CC'地址,但不發送到'TO'。很可能是由於我的代碼中有錯誤。

在電子郵件確認收到,它只顯示從部分和「到」部分是空的,如下面所示:

From: [email protected] 
To: *This space is empty* 
CC: [email protected] 

任何人的幫助能指出我要去的地方錯了嗎?我附上了下面的代碼。

//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 = "[email protected]"; 
$cc = "[email protected]"; 
//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: **[email protected]** 
To: **$_SESSION['od_email'];** 
CC: **[email protected]** 

預先感謝給予

+0

您確定$ _SESSION ['od_email']有值嗎?如果你'var_dump($ _ SESSION ['od_email'])''得到了什麼? –

+0

完成這件事情後,會發生同樣的事情。我相信它確實有價值。該字段數據庫中的每個部分都有一個有效的測試電子郵件地址。但是,我希望它在下訂單後選擇訂單客戶訂單的電子郵件地址。這就是爲什麼我假設我使用會話功能。 – JUM

+0

我仍然不確定爲什麼要設置$ _SESSION ['od_email'],然後在最後取消設置。不過,看起來最可能的問題是$ sendto(和$ _SESSION ['od_email'])是NULL或空白。 'exit($ sendto);'在'mail()'之前應該打印它的值。此外,它看起來像你的查詢應該看起來像這樣:「SELECT od_email從tbl_order WHERE id = $ _ SESSION ['orderId']」,所以你不要選擇每一行。 –

回答

0

它看起來像這個問題已經回答了在評論任何幫助 - 但在充分:

$_SESSION['od_email']變量未被填充,因此To地址留空。

解決的辦法是添加查詢從最近的訂單獲取用戶名:

$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] 
0

您正在使用的會話。並且在使用它之前你還沒有開始任何會話。

開始會話。添加到您的頁面頂部

session_start(); 
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 = "[email protected]"; 
$mail->Password = "******"; //Don't reveal password with others 
$mail->SetFrom("[email protected]"); 
$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>"; 
} 

?> 

在這裏你還可以添加附件,如果any.The class.phpmailer.php是增加了庫和coonect.php是連接文件連接到數據庫。您還可以查看以下輸出:link