0

我刚刚在联系表单中添加了一个电话号码输入,我想这样做,所以如果用户选择在我们的表单中输入他们的电话号码,我想在发送的电子邮件正文中附加一些文本 + 电话号码php mail() 函数。如果用户将电话号码输入留空,我不想要额外的文字。

该脚本运行良好,直到我插入带有我想要附加的文本的 if 语句。感谢您的任何帮助,您可以提供!

<?php

$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$number = $_REQUEST['number'];
$message = $_REQUEST['message'];

   $to = "test@test.com"; 
   $email_subject = "Message from test.com";
   $email_body = "$message";

   $headers = "From: $name <$email>";

   mail($to,$email_subject,$email_body if ($number != "") { echo "Patient Phone Number: $number";},$headers);
?>
4

3 回答 3

4

您不能在函数参数中使用 if 语句,因此请使用:

<?php

  $name = $_REQUEST['name'];
  $email = $_REQUEST['email'];
  $number = $_REQUEST['number'];
  $message = $_REQUEST['message'];

  $to = "test@test.com"; 
  $email_subject = "Message from test.com";
  $email_body = "$message";

  /**
   * Append number to email body (.= adds text after the previously defined $email_body -string)
   */
  if ($number != "") { 
    $email_body .= "Patient Phone Number: $number";
  }

  $headers = "From: $name <$email>";

  mail($to,$email_subject,$email_body,$headers);

?>
于 2012-10-02T00:22:24.433 回答
2

尝试更多类似的东西:

if($number != ""){
  $email_body .= "Patient Phone Number: $number";
}

mail($to,$email_subject,$email_body,$headers);
于 2012-10-02T00:21:04.030 回答
1

尝试从邮件函数中删除 if 语句以将其分配为变量,然后将其发送到邮件函数。

于 2012-10-02T00:22:36.543 回答