2

I'm writing an emailing script for order confirmation for customers of my website. I'm having a problem where the total of the order displayed in the email is being placed on a new line, however no new line is in the code. I've attached an image of what is happening.enter image description here

Here is my code:

<?php
  if(isset($_POST['email']) && isset($_POST['name']) && isset($_POST['order'])) {
    $to = $_POST['email'];
    $name = $_POST['name'] .",";
    $order = $_POST['order'];
    $trackingNumber = $_POST['trackingNumber'];
    $totalAmount = "Your total was: $".$_POST['totalAmount'];

    if(!empty($to) && !empty($name) && !empty($order)) {

      $subject = 'From the band.it team ';
      $text = "My name is Bobby and I am a part of band.it's fulfillment Team! We recieved your order and are working to process it from our Gainesville, Florida facility. Feel free to reach out to us at any point with any comments, questions, or concerns. You can find our contact infomation at the bottom of this email.";
        $body = "Hey ".$name."\r\n".$text."\r\nYour order will contain:\r\n".$order."\r\n";
      $body = $body.$totalAmount;
      $body = $body."\r\nYou can track your package through USPS with this number:\r\n".$trackingNumber."\r\n";
      $body = $body."\r\nYou can view any order you've made with us via going to the following link with your tracking number handy!\r\nhttps://camerabandit.com/findAnOrder/";
      $body = $body."\r\n\r\nThanks again for your support of the band.it team, and reach out to us anytime by sending an email to contact@camerabandit.com";
      $headers = 'Order Confirmed';

      if (mail($to, $subject, $body, $headers)) {
        echo 'Mail has been sent';
      } else {
        echo 'Mail failed to send';
      }
    }
  }
?>
Giga Chad Coding
  • 178
  • 3
  • 12

3 Answers3

5

My best guess is that $_POST['totalAmount'] contains a line break.
You may want to trim() it:

$totalAmount = "Your total was: $".trim($_POST['totalAmount']);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
1

i would suggest a table to enhance the display of the message also check your var i think it contains \r \n you can use this to trim it

$string = trim(preg_replace('/\s\s+/', ' ', $string));
Axcel
  • 60
  • 10
1

The part of the body directly before it is:

$body = "Hey ".$name."\r\n".$text."\r\nYour order will contain:\r\n".$order."\r\n";

So that ends with

\r\n

That's where your linebreak is...

Johannes
  • 64,305
  • 18
  • 73
  • 130