为什么通过php发送的电子邮件中的文本被换成新行?

时间:2017-05-01 23:02:14

标签: php email phpmailer

我正在为我网站的客户编写电子邮件脚本以确认订单。我遇到一个问题,即电子邮件中显示的订单总数放在新行上,但代码中没有新行。我附上了正在发生的事情的图像。enter image description here

这是我的代码:

<?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';
      }
    }
  }
?>

3 个答案:

答案 0 :(得分:5)

我最好的猜测是$_POST['totalAmount']包含换行符 您可能需要trim()

$totalAmount = "Your total was: $".trim($_POST['totalAmount']);

答案 1 :(得分:1)

我会建议一个表来增强消息的显示也检查你的var我认为它包含\ r \ n你可以用它来修剪它

$string = trim(preg_replace('/\s\s+/', ' ', $string));

答案 2 :(得分:1)

身体正前方的部分是:

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

结尾
\r\n

这就是您的换行符......