I'm creating a form using Swiftmailer. The problem is, nothing from inside the form, but the PHP body appear in the email. I've also watch some tutorial from Youtube and Google, tried them but failed, nothing working yet.
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>
Here's the screen capture
What i've done wrong? Help me. (Using Bootstrap as the form)
OK, it looks like you are missing the name attribute to your form inputs.
Add the attributes, and then your form should work, e.g.:
<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>
Problem is you have to add name attribute to input.
So instead <input type="text" class="form-control" id="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
You should use <input type="text" class="form-control" id="name" name="name" placeholder="Enter name" required data-error="NEW ERROR MESSAGE">
so your PHP script can get this value from array which comes with POST request.
Add name attribute to each input and than your script will get it after form submitting.