I have a PHP form on my site and have managed to get it working. The problem is that it sends back to my HTML file that an error occurred. I have checked the messages in webmail and they come through fine.
I think it may be to do with this line as in the console using XAMPP it shows:
Warning</b>: mail() expects parameter 3 to be string, object given in
mail('emails.contactus', $data , function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject)
{
$message->to($toEmail, $toName);
$message->from($fromEmail, $fromName);
$message->subject($subject);
});
I can also see this error:
Undefined variable: data in
I don't have a mail server so it's hard to fully debug on XAMPP. But using the live site the mail goes through.
HTML:
<div class="col-md-8 contact-form-wrapper">
<div class="alert alert-style-1 information-box-home success-alert">
<div class="alert-icon"><i class="ico-ok"></i></div><!-- end of alert thumb -->
<div class="alert-contents">
<h6 class="alert-title">Success Message</h6>
<p>Thank you for contacting us, you will hear from us soon!</p>
</div><!-- end of alert contents -->
</div><!-- end of alert -->
<div class="alert alert-style-1 information-box-home fail-alert">
<div class="alert-icon"><i class="ico-cancel"></i></div><!-- end of alert thumb -->
<div class="alert-contents">
<h6 class="alert-title">Failure Message</h6>
<p>Your message has not come through please contact for more details</p>
</div><!-- end of alert contents -->
</div><!-- end of alert -->
<form action="contact-form.php" method="POST" class="contact-form">
<ul class="row">
<li class="col-md-6 form-item">
<label for="contact-name"><i class="ico-male"></i></label>
<input type="text" name="contact-name" class="contact-name" id="contact-name" value="Your Name" onblur="if(this.value=='')this.value='Your Name'" onfocus="if(this.value=='Your Name')this.value=''">
</li>
<li class="col-md-6 form-item">
<label for="contact-email"><i class="ico-email"></i></label>
<input type="email" name="contact-email" class="contact-email" id="contact-email" value="Your Email" onblur="if(this.value=='')this.value='Your Email'" onfocus="if(this.value=='Your Email')this.value=''">
</li>
<li class="col-md-12 form-item">
<label for="contact-message"><i class="ico-bubble"></i></label>
<textarea name="contact-message" class="contact-message" id="contact-message" data-placeholder="Your message"></textarea>
</li>
<li class="col-md-12 form-item">
<input type="submit" name="contact-btn" class="contact-btn general-link" id="contact-btn" value="Send Your Message">
</li>
</ul>
</form><!-- end of contact form -->
</div><!-- end of contact form wrapper -->
Ajax:
<!-- Ajax Contact Form -->
jQuery(document).ready(function($){
// send message
var form = $(".contact-form");
$('.fail-alert').hide();
$('.success-alert').hide();
form.on( "submit", function( event ) {
$(".contact-btn").button('loading');
event.preventDefault();
$.ajax({
type: "POST",
url: form.attr( 'action' ),
data: form.serialize(),
success: function( response ) {
console.log(response)
if(response == "success"){
$('.fail-alert').slideUp();
$('.success-alert').slideDown();
}
else{
$('.success-alert').slideUp();
$('.fail-alert').slideDown();
}
console.log( response );
$(".contact-btn").button('reset');
}
});
});
});
PHP:
<?php
if(!isset($_POST['submit'])) {
$fromEmail = strip_tags($_POST['contact-email']);
$fromName = strip_tags($_POST['contact-name']);
$themessage = strip_tags($_POST['contact-message']);
$themessage = $themessage."The Sender Is ( ".$fromName." )" ;
$toEmail = 'email@email.com';
$toName = 'Name';
$subject = 'Enquiry';
mail('emails.contactus', $data , function($message) use ($toEmail, $toName, $fromEmail, $fromName, $subject)
{
$message->to($toEmail, $toName);
$message->from($fromEmail, $fromName);
$message->subject($subject);
});
$headers = 'From:' .$fromName . "\r\n" .
'Reply-To:' .$fromEmail. "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($toEmail, $subject, $themessage, $headers))
{
// Send
echo "success";
}
else{ echo "An error has be occured";
}
}
?>