PHP邮件表单显示空白响应

时间:2014-09-25 11:26:41

标签: php html forms email

我有一个模板中的PHP邮件表单,它将电子邮件发送给我,但看起来所有变量都是空白的。



<?php
	header('Content-type: application/json');
	$status = array(
		'type'=>'success',
		'message'=>'Email sent!'
	);

    $name = @trim(stripslashes($_POST['yourname'])); 
    $email = @trim(stripslashes($_POST['youremail'])); 
    $subject = @trim(stripslashes($_POST['yoursubject'])); 
    $message = @trim(stripslashes($_POST['yourmessage'])); 

    $email_from = $email;
    $email_to = 'kylef33@gmail.com';

    $body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

    $success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');

    echo json_encode($status);
    die; 
?>
&#13;
&#13;
&#13; 在这里,HTML:

<div class="col-sm-6">
    <h1>
        Contact Form
    </h1>
    <p>
        Fill out the form to enquire directly and we will get back to you as soon as possible.
    </p>
    <div class="status alert alert-success" style="display: none">
    </div>
    <form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <input name="yourname" type="text" class="form-control" required="required" placeholder="Name"> 
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <input name="youremail" type="text" class="form-control" required="required" placeholder="Email address"> 
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <textarea name="yourmessage" id="message" required class="form-control" rows="8" placeholder="Message"></textarea> 
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-danger btn-lg">Send Message</button> 
                    <input type="hidden" name="yoursubject" value="Enquiry"> 
                </div>
            </div>
        </div>
    </form>
</div>

但产生的电子邮件是:

名称:

电子邮件:

主题:

消息:

没有任何回复。我哪里错了?

编辑:

如果我在php文件中手动设置变量,则消息很好:

&#13;
&#13;
    $name = 'John'; 
    $email = 'email@email.com; 
    $subject = 'Enquiry Form'; 
    $message = 'Message here.';
&#13;
&#13;
&#13;  我认为php文件没有正确地从表单中获取变量。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

我没有看到电子邮件的标题。尝试添加这个:

$headers = "MIME-Version: 1.0\r\nFrom: $noReplay\r\nReply-To: $noReplay\r\nContent-Type: text/html; charset=utf-8";

其中$noReplay是要显示给接收方的电子邮件地址,标头作为mail()函数中的最后一个参数发送。

希望这有帮助!
继续编码,
顷。

答案 1 :(得分:0)

尝试使用简单的代码和设置内容类型:标题中的text / html可能有用:

$name = $_POST['yourname']; 
$email = $_POST['youremail']; 
$subject = $_POST['yoursubject']; 
$message = $_POST['yourmessage']; 

$email_from = $email;
$email_to = 'kylef33@gmail.com';

$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;

//set the headers 

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'From: <'.$email_from.'>' . "\r\n";

// Mail it
mail($email_to, $subject, $body, $headers);