Skip navigation
Currently Being Moderated

PHP Contact Form

Mar 27, 2013 3:54 PM

So here is my code:

<?php

$field_name = $_POST['name'];

$field_email = $_POST['email'];

$field_message = $_POST['message'];

$field_phone = $_POST['phone'];

$field_subject = $_POST['subject'];

 

 

$mail_to = 'emailhere';

$subject = 'Message from site: '.$field_subject;

 

 

$body_message = 'From: '.$field_name."\n";

$body_message .= 'E-mail: '.$field_email."\n";

$body_message .= 'Message: '.$field_message;

 

 

$headers = 'From: '.$field_email."\r\n";

$headers .= 'Reply-To: '.$field_email."\r\n";

 

 

$mail_status = mail($mail_to, $subject, $body_message, $headers);

 

 

if ($mail_status) { ?>

          <script language="javascript" type="text/javascript">

                    alert('Thank you for the message. We will contact you shortly.');

                    window.location = '../index.php?page=contactus';

          </script>

<?php

}

else { ?>

          <script language="javascript" type="text/javascript">

                    alert('Message failed. Please, send an email to emailhere');

                    window.location = '../index.php?page=contactus';

          </script>

<?php

}

?>

 

 

For some reason everything works, except I don't get the e-mail. I do have the correct e-mail I jsut removed it for this post. Anybody have any idea?

 
Replies
  • Currently Being Moderated
    Mar 27, 2013 4:01 PM   in reply to Gabriel McKenna

    You did not say whether you are testing on the local or remote server.

     

    If it is the local server, I wonder if you have the appropriate mail server.

     

    Upload the site to remote and the likelihood is that it will work.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 27, 2013 5:48 PM   in reply to Gabriel McKenna

    Try changing $headers to include the highlighted

    $headers = 'From: '.$field_email."\r\n";

    $headers .= 'Reply-To: '.$field_email."\r\n";

    $headers .= 'X-Mailer: PHP/' . phpversion();

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 27, 2013 5:53 PM   in reply to Ben Pleysier

    As a supplementary answer, I have used this in the past

    $headers.= "Content-type:  text/html; charset=iso-8859-1;";

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 27, 2013 8:42 PM   in reply to Gabriel McKenna

    I cannot fault your original code, just trying a few alternatives.

     

    This now makes me wonder if the problem lies elsewhere. Do you have a link to the site? Or as a second preference, paste the code of the form here.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 28, 2013 12:59 AM   in reply to Gabriel McKenna

    Just to confirm what Ben has posted.

     

    The php works fine when hosted locally or remotely on my server.....so the problem lies not with the php but with most likely the server configuration.

     

    It's a long shot but as a test try removing the javascript and replace them with php:

     

     

    <?php echo "Thank you for the message. We will contact you shortly.";

    header ('Location: ../index.php?page=contactus');

    ?>

     

     

    <?php echo "Message failed. Please, send an email to emailhere.";

    header ('Location: ../index.php?page=contactus');

    ?>

     

     

    Also if you have an alternative mail server you could try that would nail the problem down a bit more

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points