【问题标题】:Using php mail doesn't insert new lines使用 php 邮件不会插入新行
【发布时间】:2013-10-26 02:09:13
【问题描述】:

在以下代码中,$message 在 html 中显示时有新行(包装在 pre 标记中),但未在电子邮件中显示(我将其发送到我的 gmail):

error_reporting(E_ALL);
ini_set('display_errors', 'on');
include "../code/dbstuff.php";
$to = "me@mail.com";
$from = "";
function processMessage($arr) {
  global $from;
  $message = "Hello from me\n\n\n";
  foreach ($arr as $key => $val) {
    if (isset($_POST[$key])) {
      $message.=$val . ": " . $_POST[$key] . "\n\n";
    } else {
      $message.=$val . ": empty" . "\n\n";
    }
  }
  $message.= "Date: " . date('l F d, Y');
  from = $_POST['contact_email'];
  return $message;
}

$message = processMessage(array(
    "firstname" => "First Name",
    'lastname' => 'Last Name'
));
//save in db
$conn=new db();
$conn->insertMessage($from,$message);
mail( $to,$message,"From: $from" );
header( "Location: thankyou.php" );

【问题讨论】:

  • 一方面,您的mail() 函数缺少subject,这可能会给您带来问题。
  • 实际上就是这样,消息被塞进了主题。客户报告的问题可能是整个消息都塞进了主题中。
  • 那么...问题解决了吗?
  • @Fred-ii- 是的,这一定是问题所在。感谢您的帮助。如果您愿意提供它,我会接受它作为答案。否则我会在本周晚些时候添加它,这样这个问题就不会“悬而未决”
  • 不客气。答案已发布,干杯。

标签: php email newline


【解决方案1】:

标题缺少“subject”参数,这肯定会导致问题。

这个:

mail( $to,$message,"From: $from" );

应该读作:

mail( $to,$message,$subject,"From: $from" );

同时添加一些东西来影响:

$subject =  "Incoming message";`

【讨论】:

    【解决方案2】:

    在 HTML 邮件中,空格会被忽略。请改用<br/>
    如果你想在你的代码中使用\n,你应该使用content-type: text/plain

    【讨论】:

    • 感谢您的回复,但我不会发送 html 消息。根据文档,邮件默认为文本:php.net/manual/en/function.mail.php 我将尝试 \r\n 而不是 \n 尽管使用了原始代码 \n 并且在我更改了一些代码后客户开始抱怨。可能是他们从未测试过我编写的代码并且它已经没有显示新行。
    猜你喜欢
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多