0

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"; 

}
}

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sam
  • 1,207
  • 4
  • 26
  • 50
  • What is the error? – Jay Blanchard Jan 09 '17 at 16:21
  • I'm not sure why it is throwing an error as the mail goes through. But it comes back and in AJAX line if(response == "success"){ it isn't coming back as success so shows an unsuccessful attempt on the webpage, – Sam Jan 09 '17 at 16:24
  • [Have you watched the AJAX request / response in the browser's developer tools? Have you included the jQuery library in the project? Are there any errors reported? Are you running this on a web-server?](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard Jan 09 '17 at 16:26
  • you should look at setting your php mail to log instead of email since you do not have an email server set up – happymacarts Jan 09 '17 at 16:27
  • Not sure why you are using an anonymous function as the 3rd parameter to `mail` ? `mail('emails.contactus', $data , function($message)` http://php.net/manual/en/function.mail.php – John Joseph Jan 09 '17 at 16:30
  • Your code is written oddly to me. In your AJAX you serialize your data but then you break it apart again. Try this AJAX method. It is simple, clean and should help you debug easily. http://stackoverflow.com/questions/28458307/how-to-pass-multiple-variables-to-php-with-jquery – technology101010 Jan 09 '17 at 16:37

2 Answers2

0

$data isn't defined in your example code and you're passing that as a second parameter, the second param should be a subject line, not sure if you set it earlier in the page and you've just not included it here?

You could just test it with a placeholder like:

$data = 'My subject line';
PhilS
  • 365
  • 2
  • 8
0

There's a couple of things wrong with your script.

You are calling the mail function twice, the errors you are getting in the log is from the first call you make to mail with an undefined variable as the 2nd parameter and an incorrect 3rd parameter type:

mail('emails.contactus', $data , function($message) use ($toEmail, ...)
{
    $message->to($toEmail, $toName);

    $message->from($fromEmail, $fromName);

    $message->subject($subject);
});

As that error is only a warning, the script continues and sends the mail through the second call you have to mail() :

if(mail($toEmail, $subject, $themessage, $headers))

I suspect you want to remove that first mail() call completely, it looks like it is supposed to be a call to some other function which happens to be called mail also (or you haven't changed the name of it to the correct one)

John Joseph
  • 921
  • 5
  • 14