1

I am having trouble with this email form. there is no php error code or anything, the email just never gets delivered... this entire code sample is an included file on several pages in my site. the form submits to whatever page it's on, and works, except for the fact that I don't know why it wouldn't send the email. it doesn't seem to process the else statement that contains the code to send the email.

<div class="green_box" id="contact_us">
<h3>CONTACT US</h3>
<div class="white_box">

<?php
if ($_POST['submitted']==1) {  
    $errormsg = ""; //Initialize errors  
    if ($_POST[your_name]){  
        $your_name = $_POST[your_name]; 
    }  
    else {  
        $errormsg = "You did not enter your Name";  
    }  
    if ($_POST[your_email]){  
        $your_email = $_POST[your_email];
    }  
    else {  
        if ($errormsg){ //If there is already an error, add next error  
            $errormsg = $errormsg . " or your Email";  
        }else{  
            $errormsg = "You did not enter your Email";  
        }  
    }
    if ($_POST[your_message]){  
        $your_message = $_POST[your_message];
    }  
    else {  
        if ($errormsg){ //If there is already an error, add next error  
            $errormsg = $errormsg . " or your Message";  
        }else{  
            $errormsg = "You did not enter your Message"; 
        }  
    }
    if (strlen($errormsg) > 1) {
        echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>";
    }
    else {
        $email_to = "willyfresh@gmail.com"; // recipient inbox
        $email_subject = "Fore A Partners Website Contact Form";
        $email_message = "Form details below.\n\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($your_name)."\n";
        $email_message .= "Email: ".clean_string($your_email)."\n";
        $email_message .= "Comments: ".clean_string($your_message)."\n";

        $headers = 'From: '.$your_email."\r\n".
        'Reply-To: '.$your_email."\r\n" .
        'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);  
        echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>";
    }
}
?>

<form name="contactform" method="post">
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p>
<p>Email<br><input type="text" name="your_email" maxlength="80"  size="45"></p>
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p>
<input type="hidden" name="submitted" value="1">
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p>
</form>
</div>
</div>
  • In PHP, putting `@` in front of a function suppresses error messages – Tom Smilack Oct 18 '12 at 01:23
  • You may also consider adding your code with an if condition like `if ( empty( $_POST ) === false ) { your code }` So it executes on the right momment – PatomaS Oct 18 '12 at 01:33

1 Answers1

1

UPDATE

There are several reasons why it may not be working properly. What type of server are you running on? You may need to configure the SMTP property in your php.ini, but it's hard to say without knowing more.

I personally prefer the PEAR mail solution, it's more robust and allows me to easily configure SMTP information. I cleaned up your code and implemented a sample PEAR mail script that works for me. Most likely you'll already have PEAR mail installed, however, if you need to download and install it go here: http://pear.php.net/package/Mail/download

<?php
if ($_POST) {
    if ($_POST['submitted']==1) {  
        $errormsg = ""; //Initialize errors  
        if ($_POST['your_name']){  
            $your_name = $_POST['your_name']; 
        }  
        else {  
            $errormsg = "You did not enter your Name";  
        }  
        if ($_POST['your_email']){  
            $your_email = $_POST['your_email'];
        }  
        else {  
            if ($errormsg){ //If there is already an error, add next error  
                $errormsg = $errormsg . " or your Email";  
            }else{  
                $errormsg = "You did not enter your Email";  
            }  
        }
        if ($_POST['your_message']){  
            $your_message = $_POST['your_message'];
        }  
        else {  
            if ($errormsg){ //If there is already an error, add next error  
                $errormsg = $errormsg . " or your Message";  
            }else{  
                $errormsg = "You did not enter your Message"; 
            }  
        }
        if (strlen($errormsg) > 1) {
            echo "<p><strong>" . $errormsg . ".</strong><br>Please try again.</p>";
        }
        else {
             // recipient inbox
            $email_subject = "Fore A Partners Website Contact Form";
            $email_message = "Form details below.\n\n";

            function clean_string($string) {
              $bad = array("content-type","bcc:","to:","cc:","href");
              return str_replace($bad,"",$string);
            }

            $email_message .= "Name: ".clean_string($your_name)."\n";
            $email_message .= "Email: ".clean_string($your_email)."\n";
            $email_message .= "Comments: ".clean_string($your_message)."\n";            

            // INCLUDE PEAR MAIL
            require_once "Mail.php";
            require_once('Mail\mime.php');

            // CONFIGURE SMTP SETTINGS
            $email_to = "willyfresh@gmail.com";
            $sender = "emailfrom@example.com";
            $host = "mail.example.com";
            $username = "emailfrom@example.com";
            $password = "password";

            $crlf = "\n";       
            $headers = array(
                'From' => $sender,
                'To' => $email_to,
                'Reply-To' => $your_email,
                'Subject' => $email_subject
            );  
            // Creating the Mime message
            $mime = new Mail_mime($crlf);   
            // Setting the body of the email
            $mime->setTXTBody($your_message);
            $mime->setHTMLBody($your_message);  
            $body = $mime->get();
            $headers = $mime->headers($headers);    
            // Sending the email
            $mail =& Mail::factory('smtp',
            array ('host' => $host,
                'auth' => true,
                'username' => $username,
                'password' => $password
            ));
            $mail->send($email_to,$headers,$body);

            echo "<p>Thank you for contacting us. We will be in touch with you very soon.</p>";
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div class="green_box" id="contact_us">
<h3>CONTACT US</h3>
<div class="white_box">
<form name="contactform" method="post">
<p>Name<br><input type="text" name="your_name" maxlength="80" size="45"></p>
<p>Email<br><input type="text" name="your_email" maxlength="80"  size="45"></p>
<p>Message<br><textarea name="your_message" maxlength="1000" rows="6" cols="30"></textarea></p>
<input type="hidden" name="submitted" value="1">
<p><input type="image" src="../btn_submit.png" alt="Submit" name="submit"></p>
</form>
</div>
</div>
</body>
</html>

The first thing i see is you need to grab your posts like $_POST['your_name'] as opposed to $_POST[your_name]

Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24
  • 1
    While it's a bad practice, it won't actually cause error. PHP will intentionally misinterpret what it sees as undefined constants as strings, convert `your_name` into the string `"your_name"`, and issue an `E_NOTICE`. – Michael Berkowski Oct 18 '12 at 02:07
  • Thank you Jeffery! Unfortunately I'm not familiar with PEAR. The what is PEAR page isn't helping any... http://pear.php.net/manual/en/about.pear.php – Willy Fresh Oct 18 '12 at 18:17
  • PEAR is a PHP extension, you can test the above script or use the following code to see if you have it installed: – Jeff Wooden Oct 18 '12 at 18:24
  • I can't decide if PEAR is a replacement for the current PHP version running or If it's included in all PHP or an add on library within a php page. I understand that Mail is a package on it's own... is that just a php file included or do I need to make server changes? I am practicing on MAMP currently. The target server for the work I'm doing is a GoDaddy server. I am pretty new at this, and am crashing through it, learning as I go. – Willy Fresh Oct 18 '12 at 18:32
  • It is a PHP extension, you can use this guide to install it on MAMP: http://stackoverflow.com/questions/5510734/install-pear-on-mamp – Jeff Wooden Oct 18 '12 at 18:37
  • phpinfo revealed the following line: Phar based on pear/PHP_Archive, original concept by Davey Shafik. – Willy Fresh Oct 18 '12 at 18:41
  • i had issues installing it on MAMP. are you sure this is the best option for me? – Willy Fresh Oct 18 '12 at 23:13
  • It is my preferred method, especially with the SMTP authentication flexibility... you can still attempt to use the PHP mail function... here is an answer i found for the mail function and MAMP: http://stackoverflow.com/questions/8461020/how-can-i-send-mail-with-phps-mail-function-and-mamp-pro – Jeff Wooden Oct 18 '12 at 23:57