我是PHP的新手,正在尝试使用php中的在线订购网站。尝试使用'cc'发送确认电子邮件时遇到问题。
每次处理订单时,订单总是发送到指定为'$ from'的'CC'地址,发件人(from)是'$ 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**
提前感谢您提供的任何帮助
答案 0 :(得分:2)
当您要设置CC时,您将覆盖$ header中的内容。尝试:
$headers = "From:" . $from . "\r\n";
$headers.= "Cc:" . $cc;
如果这没有帮助,那么mail
上总会有PHP的文档。
答案 1 :(得分: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']
来它不会工作,它会将其视为无效的电子邮件并返回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);
}
答案 2 :(得分:1)
他不能只添加一个@mail吗?比如......
$mail_sent = @mail( $od_email, $subject, $message, $headers );
或
mail( $od_email, $subject, $message, $headers );
替换任何变量。
一直为我工作。