表单中的内容未发送到webmail

时间:2017-02-14 09:13:56

标签: php forms swiftmailer

我正在使用Swiftmailer创建表单。问题是,表单内部没有任何内容,但PHP正文出现在电子邮件中。我也看过Youtube和谷歌的一些教程,尝试过但失败了,没有任何工作。

HTML

<form id="contactForm" method="POST" action="form-process.php">
            <div class="row">
                <div class="form-group col-md-6">
                    <label class="h4">Your Name</label>
                    <input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
                    <div class="help-block"></div>
                </div>
                <div class="form-group col-md-6">
                    <label class="h4">Your Email</label>
                    <input type="text" class="form-control" id="email" placeholder="Enter name" required>
                    <div class="help-block"></div>
                </div>
            </div>
            <div class="form-group">
                    <label class="h4">Your Message</label>
                    <textarea id="message" class="form-control" rows="5" placeholder="Enter your message" required></textarea>
                    <div class="help-block"></div>
                </div>
                <button type="submit" id="form-submit" class="btn btn-success btn-lg pull-right ">Submit</button>
        <div id="msgSubmit" class="h3 text-center hidden"></div>
        <div class="clearfix"></div>
        </form>

PHP

<?php

require_once 'swiftmailer-5.x/lib/swift_required.php';

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 465, 'ssl')
  ->setUsername('mysite@domain.com')
  ->setPassword('password')
  ;

// Get data 

$name=filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email=filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$message=filter_var($_POST['message'], FILTER_SANITIZE_STRING);

$data = "Name: " . $name . "<br />" .
        "Email: " . $email . "<br />" .
        "Message: " . $message . "<br />";

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

// Create a message
$message = Swift_Message::newInstance('Feedback Form')
  ->setFrom(array('mysite2@domain.com' => 'Feedback Form'))
  ->setTo(array('mysite@domain.com' => 'Feedback Form'))
  ->setBody($data, 'text/html')
  ;

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

?>
<p>Thank you for give us your feedback. We will respond to your feedback as soon as possible.</p>
<p>Please click <button type="btn btn-success"><a href="contact.html">Here</a></button> to return to the site.</p>

这是屏幕截图

enter image description here

我做错了什么?帮我。 (使用Bootstrap作为表单)

2 个答案:

答案 0 :(得分:3)

好的,看起来您缺少表单输入的name属性。

添加属性,然后您的表单应该有效,例如:

<input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
<input type="text" class="form-control" id="email" name="email" placeholder="Enter name" required>
<textarea id="message" class="form-control" name="message" rows="5" placeholder="Enter your message" required></textarea>

答案 1 :(得分:1)

问题是您必须为输入添加名称属性。
所以相反<script> var currentTime = new Date(), hours = currentTime.getHours(), minutes = currentTime.getMinutes(); if (minutes < 10) { minutes = "0" + minutes; } document.write(hours + ":" + minutes) </script>
您应该使用<input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">,这样您的PHP脚本就可以从POST请求附带的数组中获取此值。
为每个输入添加name属性,而脚本将在表单提交后获取它。