Reputation: 131
I'm needing to be able to fill in a form on a password protected webpage that then sends the information to the end user but it also needs to send a copy to the generic sales email address.
I've been at this for a while and have managed to get it to send emails but they only end up sending to the noreply address.
index.php
<form method="post" name="process.php" action="process.php">
<p>Customer Name:</p><br><input type="text" name="name">
<p>Customer Email Address:</p><br><input type="text" name="email">
<p>Customer Order Number:</p><br><input type="text" name="order">
<p>Customer Order Date:</p><br><input type="text" name="date">
<p>Total Paid:</p><br><input type="text" name="cost">
<p>Tracking Number:</p><br><input type="text" name="tracking">
<br>
process.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$order = $_POST['order'];
$date = $_POST['date'];
$cost = $_POST['cost'];
$tracking = $_POST['tracking'];
?>
<?php
$email_from = "[email protected]";
$email_subject = "Your order details";
$email_body = "Hello,\n $name.\n".
"Your order number is:\n $order".
"Your order was placed on:\n $date".
"Your total cost is:\n $cost".
"Your tracking number is:\n $tracking".
"You can track your order on our website.\n".
"Thanks,\n examplecompany \n".
$to = $email.
$bcc = "[email protected]".
$headers = "From: [email protected] \r\n";
$headers .= "Reply-To: [email protected] \r\n";
mail($to,$email_subject,$email_body);
?>
<p>Sending Email…</p>
<meta http-equiv="refresh" content="10000;URL='https://staff.example.com/fulfilment/confirmorder/#useremail'"/>
What should happen is that the email should send to the user and a copy (using BCC) to the sales@ email address. Currently the email no longer sends and the inputs from the form aren't included in the order.
The code above has the form (which is on a separate page in the same directory) and the process.php document.
Any help would be appreciated to get the email to send to both the user and the sales account and also include the actual form inputs.
UPDATE: And with a HTML email body
With a HTML email body it's easier for me to include links and the company logo.
$email_body = '
<body>
<div>
Hello,<?php $name ?><br><br>
Your order number is: <strong><?php $order ?></strong><br>
Your order was placed on: <strong><?php $date ?></strong><br>
Your total cost is: <strong>£<?php $cost ?></strong><br>
Your tracking number is: <strong><?php $tracking ?></strong><br>
You can track your order on our website <a href="https://example.com/track">here</a>.<br><br>
Thanks,<br>examplecompany.<br>
<a href="https://example.com"><img src="https://example/assets/images/logo.png" alt="examplecompany logo" width="200px"></a>
</div>';
Everything works in a text email apart from displaying the PHP $name
tags etc.
Upvotes: 0
Views: 89
Reputation: 137
It seems that here: $to = $email.
and here: $bcc = "[email protected]".
shouldn't be a following dots, but a semicolons. To send to Bcc
you have to add one more header like this: $headers .= "Bcc: [email protected]\r\n";
And here should be a semicolon in the end instead of the dot:
$email_body = "Hello,\n $name.\n".
"Your order number is:\n $order".
"Your order was placed on:\n $date".
"Your total cost is:\n $cost".
"Your tracking number is:\n $tracking".
"You can track your order on our website.\n".
"Thanks,\n examplecompany \n".
So below you can see the working code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$order = $_POST['order'];
$date = $_POST['date'];
$cost = $_POST['cost'];
$tracking = $_POST['tracking'];
$email_from = "[email protected]";
$email_subject = "Your order details";
$email_body = "Hello,\n $name.\n".
"Your order number is:\n $order".
"Your order was placed on:\n $date".
"Your total cost is:\n $cost".
"Your tracking number is:\n $tracking".
"You can track your order on our website.\n".
"Thanks,\n examplecompany \n";
$to = $email;
$headers = "From: [email protected] \r\n";
$headers .= "Bcc: [email protected] \r\n";
$headers .= "Reply-To: [email protected] \r\n";
mail($to, $email_subject, $email_body, $headers);
?>
UDATE:
To include variables in PHP string, you need to use double quotes "
instead of single ones '
. But in this case you need to escape any double quotes that neccesary for your HTML code, to do this you have to put back slash before a double quote, like thhis: \"
. So below is the working code for your PHP string with HTML tags:
$email_body = "
<body>
<div>
Hello, $name<br><br>
Your order number is: <strong> $order</strong><br>
Your order was placed on: <strong> $date</strong><br>
Your total cost is: <strong>£ $cost</strong><br>
Your tracking number is: <strong> $tracking</strong><br>
You can track your order on our website <a href=\"https://example.com/track\">here</a>.<br><br>
Thanks,<br>examplecompany.<br>
<a href=\"https://example.com\"><img src=\"https://example/assets/images/logo.png\" alt=\"examplecompany logo\" width=\"200px\"></a>
</div>";
Upvotes: 1
Reputation: 329
It seems your $bcc
variable isn't used anywhere. You can add the contents to the header, which should help. It would look like this: $headers .= "Bcc: [email protected]\r\n";
You could also send the mail twice, though it generates a bit more overhead:
mail($to,$email_subject,$email_body);
mail($bcc,$email_subject,$email_body);
I hope this helps.
Upvotes: 0