PHP mail()函数发送空变量

时间:2014-06-26 10:18:17

标签: php html

出于某种原因,当我收到电子邮件时,姓名,电子邮件和消息显示为空。

HTML

<form method="post" action="send.php" class="form-horizontal">
<fieldset>
    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="name">Name</label>
        <div class="col-md-4">
            <input id="name" name="name" type="text" class="form-control input-md" required="">
        </div>
    </div>
    <!-- Text input-->
    <div class="form-group">
        <label class="col-md-4 control-label" for="email">Email</label>
        <div class="col-md-4">
            <input id="email" name="email" type="email" class="form-control input-md" required="">
        </div>
    </div>
    <!-- Textarea -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="message">Message</label>
        <div class="col-md-4">
            <textarea class="form-control" id="message" name="message"></textarea>
        </div>
    </div>
    <!-- Button -->
    <div class="form-group">
        <label class="col-md-4 control-label" for="submit"></label>
        <div class="col-md-4">
            <button id="submit" name="submit" type="submit" value="Submit" class="btn btn-outline-inverse btn-lg">Submit</button>
        </div>
    </div>
</fieldset>
</form>

PHP代码简化示例

 <?php

$emailto  = "example@example.com";
$subject  = "Example subject";
$name     = Trim(stripslashes($_POST['name']));
$email    = Trim(stripslashes($_POST['email']));
$message  = Trim(stripslashes($_POST['message']));
$headers  = "From: example@example.com\r\n";
$headers .= "Reply-To: example@example.com\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";

$body = "<p>You have received a new message:</p>
              <p><strong>Ime: </strong> {$name} </p>
              <p><strong>Email Address: </strong> {$email} </p>
              <p><strong>Poruka: </strong> {$message} </p>";

$success = mail($emailto, $subject, $body, $headers);


if ($success){
    print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">";
}
else{
    print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>

由于某些原因,当我从此联系表单/邮件功能收到电子邮件时,变量$ name,$ email和$ message显示为空。其余部分正确显示。

3 个答案:

答案 0 :(得分:0)

您还需要关闭“$body变量中的”。

$body = "<p>You have received a new message:</p>
         <p><strong>Ime: </strong> {$name} </p>
         <p><strong>Email Address: </strong> {$email} </p>
         <p><strong>Poruka: </strong> {$message} </p>";

答案 1 :(得分:0)

问题出在我的htaccess文件而不是代码中,我有一些行删除了estethics的php和html扩展,所以现在一切正常。

答案 2 :(得分:-1)

试试这个。

$body = "<p>You have received a new message:</p>
         <p><strong>Ime: </strong> ".$name." </p>
         <p><strong>Email Address: </strong> ".$email." </p>
         <p><strong>Poruka: </strong> ".$message." </p>";