PHP - 如何在mail()主题中使用空格?

时间:2014-06-06 14:27:57

标签: php email

如何发送包含主题空格的电子邮件?

这是问题所在;如果我尝试发送这样的电子邮件(如下),在主题中没有空格,它将成功发送:

mail("$email",ThanksforSubscribe,$name,"From: address@gmail.com\n");

...但是如果我在主题中放置空格则会失败:

mail("$email",Thanks for Subscribe,$name,"From: address@gmail.com\n");

我收到以下错误:

  

解析错误:语法错误,意外'对于'第23行的subscribe2.php中的(T_FOR)

完整代码:

<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  Name: 
    <input type="text" name="name"><br>

  Email: <input type="text"" name="email"></textarea><br>
  <input type="submit" name="submit" value="Submit">
  </form>
  <?php 
} else {    // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["email"])) {
    $email = $_POST["email"]; // sender
    $name = $_POST["name"];
    // message lines should not exceed 70 characters (PHP rule), so wrap it
    $name = wordwrap($name, 70);
    // send mail
    mail("yourname@googlemail.com",subscribe,$name,"From: $email\n");
    mail("$email",ThanksforSubscribe,$name,"From: yourname@gmail.com\n");
    echo "Thank you ";
  }
}
?>

3 个答案:

答案 0 :(得分:2)

 mail($email,"Thanks for Subscribe",$name,"From: yourname@gmail.com\n");

把它放入&#34;&#34;或者&#39;&#39;。像这样,PHP知道这是一个String。

答案 1 :(得分:1)

您需要将其放在一个字符串中:

mail($email, 'Thanks for subscribing.', $name, "From: example@gmail.com\n");

另外值得注意的是,您不需要将第一个参数放在字符串中,因为它只是一个变量。

答案 2 :(得分:1)

尝试:

mail($email,"Thanks fo Subscribe", $name, "From: example@gmail.com\n");