联系表格不起作用(PHP)

时间:2014-11-17 03:35:03

标签: php html

我在页面上有一个联系表单,应该将详细信息/消息发送到电子邮件地址。你可以在这里查看它(它在页脚中):http://tedsrestaurant.com/index-working.html

HTML

<form id="contact-form" class="row style1" action="#" method="post">
    <div class="col-xs-12 col-sm-4">
        <div class="form-group">
            <input name="name" type="text" id="name" placeholder="Your name" required />
        </div>
        <div class="form-group">
            <input name="number" type="text" id="number" placeholder="Your number" required />
        </div>
        <div class="form-group">
            <input name="email" type="email" id="email" placeholder="Your e-mail" required />
        </div>
    </div>

    <div class="col-xs-12 col-sm-8">
        <textarea name="message" rows="4" class="h130" id="message" placeholder="Write your message..." required></textarea>
    </div>

    <div class="clear"></div>

    <div class="col-md-9"><p>* When booking the <strong>Food Truck</strong>, please write date, time and number of guests into your message.</p></div>
    <div class="col-md-3 right">
        <input class="submit btn" type="submit" value="Send a message" />
        <input class="modal btn hidden" type="button" value="Send a message" name="" data-toggle="modal" data-target="#modalBox" />
    </div>

    <!-- Modal window -->
    <div class="modal fade bs-modal-md" id="modalBox" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-md">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Contact form</h4>
                </div>
                <div class="modal-body"></div>
                <div class="modal-footer">
                    <button type="button" class="btn right" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</form>

PHP就是:

<?php

//******** Email settings ********//
//your email
$email_to = "myemail@gmail.com";
//subject of email
$email_subject = "This is a message";
//message after success send mail
$email_send = "<div class='alert alert-success'>
<strong>Contact form was sent successfully.</strong>
<br/>Thank you for contacting us. We will be in touch with you very soon.</div>";

if($_POST) {
    //******** Email data ********//
    $cust_name = $_POST['name'];
    $cust_number = $_POST['number'];
    $cust_email = $_POST['email'];
    $cust_message = $_POST['message'];

    $header = "From:" . $cust_email . "\r\n";
    $header .= 'MIME-Version: 1.0' ."\r\n";
    $header .= 'Content-Type: text/html; charset=UTF-8' . "\r\n";
    $body  = "<strong>Customer email:</strong> " . $cust_email . "<br/>";
    $body .= "<strong>Customer number:</strong> " . $cust_number . "<br/>";
    $body .= "<strong>Customer name:</strong> " . $cust_name . "<br/>";
    $body .= "<strong>Message:</strong> " . $cust_message;

    if($cust_name && $cust_number && $cust_email && mail($email_to, $email_subject, $body, $header)) {
        echo $email_send;
        echo '<script>$("#contact-form").each(function(){
                    this.reset();
                });
              </script>';
    } else {
        echo "<div class='alert alert-warning'><strong>Error sending mail. Please fill the contact form correctly or contact website administrator.</strong></div>";
    }

}

一切看起来都正确吗?仅供参考,网站上的PHP有一个真实的电子邮件地址,而不是myemail@gmail.com。一旦我点击提交按钮,我就会被带到同一页面,但我的电子邮件中没有任何内容。

2 个答案:

答案 0 :(得分:0)

您收到的输出是多少? email_sent或错误消息?

如果你没有得到我认为的情况,你应该使用 if($_SERVER['REQUEST_METHOD'] == "POST")代替if($_POST)来检查请求类型是否为POST并继续进行。

答案 1 :(得分:0)

我建议使用SwiftMailer

require_once 'lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
  ->setUsername('your username')
  ->setPassword('your password')
  ;

/*
You could alternatively use a different transport such as Sendmail or Mail:

// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');

// Mail
$transport = Swift_MailTransport::newInstance();
*/

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
  ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);