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 protected]';
    $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"; 

}
}

?>
6

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.