我刚刚在联系表单中添加了一个电话号码输入,我想这样做,如果用户选择在我们的表单中输入他们的电话号码我想在电子邮件的正文中附加一些文字+电话号码发送出来的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);
?>
答案 0 :(得分: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);
?>
答案 1 :(得分:2)
尝试更多内容:
if($number != ""){
$email_body .= "Patient Phone Number: $number";
}
mail($to,$email_subject,$email_body,$headers);
答案 2 :(得分:1)
尝试删除if语句out of mail函数以将其指定为变量,然后将其发送到mail函数。