Sending Email in PHP

I am quite new to PHP and am experimenting with an online ordering website in php. I have come across a problem when trying to send a confirmation email using a ‘cc’.

Every time an order is processed, the order always sends to the ‘CC’ address specified but does not send to the ‘TO’. Most likely due to an error in my code.

In the email confirmation received, it only displays the from section and the ‘to’ section is empty as demonstrated below:

[php]From: [email protected]
To: This space is empty
CC: [email protected][/php]
Can anyone help to point out where I am going wrong? I have attached the code below.

[php]//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’]);[/php]

What I need for it to show is:

[php]From: [email protected]
To: $_SESSION[‘od_email’];
CC: [email protected][/php]

Thanks in advance for any help given

You need “To:” in the headers:

[php]
//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”;
$headers .= “To: “.$_SESSION[‘od_email’].”\r\n”;
//bcc header going to the restaurant
$headers .= “cc:” . $cc . “\r\n”;
[/php]

One thing, why are you using $_SESSION[‘od_email’] ? You could simply use $data[‘od_email’]. You also don’t need to set the $sendto variable so your send would be:

[php]
$mail_sent = @mail( $data[‘od_email’], $subject, $message, $headers );
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service