发送电子邮件不能与bootstrap一起使用

时间:2015-07-22 14:25:15

标签: php html forms twitter-bootstrap email

我正在尝试使用boothstrap填写联系我们表单时发送电子邮件。我收到了一封电子邮件,但其中没有任何信息的空电子邮件已填满。请帮助我,我的错误。

这是我的PHP。

<?
if(isset($_POST['sendemail'])) {     

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "contact.hanif@gmail.com";

$email_subject = "General Inquery";
$name = $_POST['name']; 
$email = $_POST['email']; 
$subject =$_POST['subject']; 
$message = $_POST['message'];      

function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}
$email_message .= "First Name: ".clean_string($name)."\n";    

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "subject: ".clean_string($subject)."\n";

$email_message .= "message: ".clean_string($message)."\n";

$headers = 'From: '.$email_from."\r\n".

   'Reply-To: '.$email_from."\r\n" .

  'X-Mailer: PHP/' . phpversion();

   @mail($email_to, $email_subject, $email_message, $headers);  

?>

HTML:

<section id="contact-page">
    <div class="container">
        <div class="center">        
            <h2>Drop Your Message</h2>
            <p class="lead">Please feel free to contact us if you have any query.</p>
        </div> 
        <div class="row contact-wrap"> 
            <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">
                <div class="col-sm-5 col-sm-offset-1">
                    <div class="form-group">
                        <label>Name *</label>
                        <input type="text" name="name" class="form-control" required="required">
                    </div>
                    <div class="form-group">
                        <label>Email *</label>
                        <input type="email" name="email" class="form-control" required="required">
                    </div>
                    <div class="form-group">
                        <label>Phone</label>
                        <input type="number" class="form-control">
                    </div>
                    <div class="form-group">
                        <label>Company Name</label>
                        <input type="text" class="form-control">
                    </div>                        
                </div>
                <div class="col-sm-5">
                    <div class="form-group">
                        <label>Subject *</label>
                        <input type="text" name="subject" class="form-control" required="required">
                    </div>
                    <div class="form-group">
                        <label>Message *</label>
                        <textarea name="message" id="message" required="required" class="form-control" rows="8"></textarea>
                    </div>                        
                    <div class="form-group">
                        <button type="submit" name="submit" class="btn btn-primary btn-lg" required="required">Submit Message</button>
                    </div>
                </div>
            </form> 
        </div><!--/.row-->
    </div><!--/.container-->
</section><!--/#contact-page-->

1 个答案:

答案 0 :(得分:0)

我不认为,这是您的完整代码,但我可以看到您从未设置$_POST['sendemail'],因此if(isset($_POST['sendemail'])) {永远不会成真。

修改

只需看下面的代码,我发表了3条评论。

<?php
// 1. -------------↓ Change sendemail to submit
if(isset($_POST['submit'])) {         

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "contact.hanif@gmail.com";

$email_subject = "General Inquery";
$name = $_POST['name']; 
$email = $_POST['email']; 
$subject =$_POST['subject']; 
$message = $_POST['message'];      

function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}

// 2. --↓  Add $email_message = '';
$email_message = '';    
$email_message .= "First Name: ".clean_string($name)."\n";

// 3. -------------------------------------↓   Change $email_from to $email     
$email_message .= "Email: ".clean_string($email)."\n";    
$email_message .= "subject: ".clean_string($subject)."\n";    
$email_message .= "message: ".clean_string($message)."\n";    
echo "message: " . $email_message . "<br>";
}

?>