Linode Forum
https://forum.linode.com/

Contact form isn't working (but it says it is!)
https://forum.linode.com/viewtopic.php?f=11&t=10517
Page 1 of 2

Author:  craven [ Tue Oct 15, 2013 6:12 am ]
Post subject:  Contact form isn't working (but it says it is!)

I've tested both my postfix

Code:
echo "Test mail from postfix" | mail -s "Test Postfix" admin@something.com

and PHP

Code:
php -a mail ('admin@something.com', "Test Postfix", "Test mail from postfix"); exit ();

and I'm receiving both the emails fine to my inbox. But my contact form @ http://www.villacalmia.co.uk/book_now.htm still isn't sending emails (even though it says it's been successfully sent) any ideas? - using the contact form from : http://www.freecontactform.com/email_form.php

I've set my contact form to send mail to testmail@primasmedia.co.uk (I have a catch-all so it should still come through to me) but I can't find any mention of the email address used in my logs (even when searching with grep)

Author:  Main Street James [ Tue Oct 15, 2013 7:08 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Have you tried setting up the headers to be sent along with the form's email? If you set the 'bounce' header you may get the email back so you can see what's going on.
Code:
Return-Path: <bounced@yourdomain.com>

Looking at their code I see that they are suppressing any errors on the line that actually sends the email:
Code:
@mail($email_to, $email_subject, $email_message, $headers);

I'd suggest removing the '@' at the beginning of the line and turning errors on. I put these two lines of code at the top of my PHP scripts while debugging (and comment them out once things are working):
Code:
error_reporting(E_ALL) ;
ini_set('display_errors', 1) ;

You should see plenty of info now (including warnings, etc, caused by other lines of that script). Focus on the stuff related to the 'mail()' line.

Author:  craven [ Tue Oct 15, 2013 7:20 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Like this? Still not getting any errors/emails back

Code:
<?php

error_reporting(E_ALL) ;
ini_set('display_errors', 1) ;

if(isset($_POST['email'])) {
     
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "testmail123@primasmedia.co.uk";
    $email_subject = "Villacalmia.co.uk contact enquiry";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required
     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $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 .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'Return-Path: <bounced@primasmedia.co.uk>'.
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers); 
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
}
?>

Author:  obs [ Tue Oct 15, 2013 7:26 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

After you submit the form what's the last few lines of /var/log/mail.log ?

Author:  craven [ Tue Oct 15, 2013 7:27 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

obs wrote:
After you submit the form what's the last few lines of /var/log/mail.log ?


Nothing is added to mail.log when I submit the form

Author:  Main Street James [ Tue Oct 15, 2013 7:40 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

It's good that you've ruled out any PHP errors. The code doesn't actually check for successes or failure of the call to mail(). The PHP mail() command returns a boolean value (true for success, false for failure).

Try adding this code, replacing the line calling mail():
Code:
$success = mail($email_to, $email_subject, $email_message, $headers);
echo 'The email ' . (($success) ? 'worked!' : 'failed.') . '<br />';

If it says it failed there's something wrong with one (or more) of the variables sent to mail().

Author:  craven [ Tue Oct 15, 2013 7:46 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Code:
The email worked!
Thank you for contacting us. We will be in touch with you very soon.


Still nothing in my inbox or logs though

Author:  obs [ Tue Oct 15, 2013 8:09 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

What's your sendmail_path php.ini setting? (You can get this from phpinfo();), make sure you test this from a web page not the command line since they use different php.ini files.

Author:  Main Street James [ Tue Oct 15, 2013 8:14 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

What happens when you use sendmail() instead of mail()? If that doesn't make a difference post your $email_to & $headers values. You may want to get them from "view page source" in your browser and put them between code tags to try to preserve some of their formatting.

Author:  craven [ Tue Oct 15, 2013 9:16 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Main Street James wrote:
What happens when you use sendmail() instead of mail()? If that doesn't make a difference post your $email_to & $headers values. You may want to get them from "view page source" in your browser and put them between code tags to try to preserve some of their formatting.


When I try and use sendmail I get

Code:
Fatal error: Call to undefined function sendmail() in /var/www/villacalmia.co.uk/send_form_email.php on line 74


My email_to and header values are posted above, or are you asking for something else?

Author:  craven [ Tue Oct 15, 2013 9:18 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

obs wrote:
What's your sendmail_path php.ini setting? (You can get this from phpinfo();), make sure you test this from a web page not the command line since they use different php.ini files.


It's

Code:
sendmail_path = /usr/local/bin/phpsendmail
in my php.ini

Where should I place it on a webpage to get it to display and output? Should I just put it in a new test.php file with nothing but that in it or incorporate it into my send_from_email.php above?

Author:  obs [ Tue Oct 15, 2013 9:36 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Well that looks wrong
Code:
sendmail_path = /usr/local/bin/phpsendmail
try changing it to /usr/sbin/sendmail -t -i

To create a phpinfo page just create a new page and put
Code:
<?php
phpinfo();
in it and load it from your browser.

Author:  craven [ Tue Oct 15, 2013 9:43 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

obs wrote:
Well that looks wrong
Code:
sendmail_path = /usr/local/bin/phpsendmail
try changing it to /usr/sbin/sendmail -t -i

To create a phpinfo page just create a new page and put
Code:
<?php
phpinfo();
in it and load it from your browser.


Okay, the path was the same when I created the php file so I went back to my php.ini file and changed it to what you specified. I'm still getting the message

Code:
The email worked!
Thank you for contacting us. We will be in touch with you very soon.


But no email is coming through, nothing in my logs either

phpinfo() here: http://villacalmia.co.uk/test.php

Author:  obs [ Tue Oct 15, 2013 9:46 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

Your sendmail_path hasn't changed make sure you're editing /etc/php5/apache2/php.ini and you've restarted apache.

Author:  craven [ Tue Oct 15, 2013 9:47 am ]
Post subject:  Re: Contact form isn't working (but it says it is!)

obs wrote:
Your sendmail_path hasn't changed make sure you're editing /etc/php5/apache2/php.ini and you've restarted apache.


Ah, my mistake! Thanks - that's it finally working :mrgreen:

Page 1 of 2 All times are UTC-04:00
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/