使用 php 邮件不会插入新行


Using php mail doesn't insert new lines

在下面的代码中,$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( $to,$message,"From: $from" );

应改为:

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

同时添加一些影响:

$subject =  "Incoming message";`

在 HTML 邮件中,空格被忽略。请改用<br/>
如果要在代码中使用'n则应使用content-type: text/plain