2

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

每次处理订单时,订单总是发送到指定为“$from”的“CC”地址,发件人(发件人)是“$to”部分中指定的地址。在收到的电子邮件确认中,它只显示 from 部分,'to' 和 'cc' 为空,如下所示:

From: **$_SESSION['od_email']**
To: *This space is empty*
CC: orders@business.co.uk

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

//THIS IS CODE FOR THE EMAIL WHICH IS AUTOMATICALLY SENT TO THE CUSTOMER AND THE BUSINESS WHEN AN ORDER IS SUCCESSFULLY COMPLETED
//define the receiver of the email
$to = $_SESSION['od_email'];
//define the subject of the email
$subject = 'Order Confirmation | Ref No for Order: '.  $_SESSION['orderId']; 
//define the message to be sent. Each line should be separated with \n
$message = 'test';
//Who the message is from
$from = "orders@business.co.uk";
$cc = "orders@business.co.uk";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From:" . $from;
//bcc header going to the to go to the business as confirmation
$headers = "cc:" . $cc;
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );

我需要它显示的是:

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

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

4

3 回答 3

2

我会全力以赴,确保你设置了你需要的一切,

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";

另外,我相信 mail() 需要一个有效的电子邮件地址,我不知道您之前是如何设置电子邮件的,$_SESSION['od_email']但是如果您正在运行测试并且确实希望$_SESSION['od_email']附带它不起作用,它会将其视为无效email 并返回 false,因此什么也不返回。要进一步诊断该问题,我们需要知道如何$_SESSION['od_email']设置

编辑

$_SESSION['od_email]'好的,我很确定问题是由您根据下面的评论进行设置引起的。

注意:我假设您的数据库是$db,但如果不是,您需要更改$db为探测器名称

让我们试试这个,看看它是如何工作的

// Get Data from the Database
$query = "SELECT `od_email` FROM `tbl_order`";
$result = $db->query($query);
while ($data = $db->fetch_array($result)) {

// Set Up the SESSION email
$_SESSION['od_email'] = $data['od_email'];

// Prepare the Email
$to = $_SESSION['od_email'];
$subject = 'Order Confirmation | Ref No for Order: '.  $_SESSION['orderId']; 
$message = 'test';
$from = "orders@business.co.uk";
$cc = "orders@business.co.uk";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";

// Send the email
mail($to,$subject,$message,$headers);

}
于 2012-06-11T00:57:55.803 回答
2

当您要设置 CC 时,您将覆盖 $headers 中的内容。尝试:

$headers = "From:" . $from . "\r\n";
$headers.= "Cc:" . $cc;

如果这没有帮助,总有 PHP 的文档在mail.

于 2012-06-11T00:26:06.733 回答
1

他不能只添加一个@mail 吗?如...

$mail_sent = @mail( $od_email, $subject, $message, $headers ); 

或者

mail( $od_email, $subject, $message, $headers );

替换任何变量。

一直为我工作。

于 2012-06-11T01:17:41.473 回答