在php发送电子邮件?

时间:2012-06-12 01:43:27

标签: php email sendmail

我是PHP的新手,正在尝试使用php中的在线订购网站。我在尝试使用'cc'发送确认电子邮件时遇到了问题。

每次处理订单时,订单始终会发送到指定的“CC”地址,但不会发送到“TO”。很可能是由于我的代码中的错误。

在收到的电子邮件确认中,它只显示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**

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

3 个答案:

答案 0 :(得分: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是连接数据库的连接文件。你也可以查看以下{{3}的输出}

答案 1 :(得分: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]

答案 2 :(得分:0)

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

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

session_start();