0

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 = "noreply@example.com";

 $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 = "sales@example.com".
  $headers = "From: noreply@example.com \r\n";

  $headers .= "Reply-To: constact@example.com \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.

phphelpplease
  • 131
  • 10
  • Where exactly are you _trying_ to send to the bcc address? I don't see `$bcc` actually _used_ anywhere. – Patrick Q Feb 01 '19 at 20:37
  • Hey, @PatrickQ I just want to send the same email to both the user who placed the order, and the sales team. If that makes sense? Apologies – phphelpplease Feb 01 '19 at 20:38
  • 1
    I would also recommend that you use one if the tried and tested mail libraries like PHPMailer, SwitfMailer or similar. They will not only give you a more verbose API, but they are also way easier to set up to use a SMTP server (which I would recommend) and your code would be independent of the server configuration (more portable). – M. Eriksson Feb 01 '19 at 20:39
  • Yes, it makes sense. What doesn't make sense is why you don't appear to make any attempt to actually do that. If you want to send an email to the sales team, why aren't you doing anything that would do that? – Patrick Q Feb 01 '19 at 20:40

2 Answers2

1

It seems that here: $to = $email. and here: $bcc = "sales@example.com". shouldn't be a following dots, but a semicolons. To send to Bcc you have to add one more header like this: $headers .= "Bcc: somebodyelse@example.com\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 = "noreply@example.com";

$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: noreply@example.com \r\n";
$headers .= "Bcc: sales@example.com \r\n";
$headers .= "Reply-To: constact@example.com \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>";
Andrew Shaban
  • 129
  • 2
  • 16
0

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: sales@example.com\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.

Asperitas
  • 339
  • 6
  • 13