1

在以下代码中,$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" );
4

2 回答 2

2

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

这个:

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

应该读作:

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

同时添加一些影响:

$subject =  "Incoming message";`
于 2013-10-26T14:31:54.343 回答
0

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

于 2013-10-26T02:11:09.323 回答