2012-06-12 48 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