Contact form isn't working (but it says it is!)
echo "Test mail from postfix" | mail -s "Test Postfix" admin@something.com
and PHP
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 @
I've set my contact form to send mail to
15 Replies
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:````
@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):````
errorreporting(EALL) ;
iniset('displayerrors', 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.
";
echo $error."
";
echo "Please go back and fix these errors.
";
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.
';
}
$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.
';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.
';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.
';
}
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);
?>
Thank you for contacting us. We will be in touch with you very soon.</bounced@primasmedia.co.uk>
@obs:
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
Try adding this code, replacing the line calling mail():
$success = mail($email_to, $email_subject, $email_message, $headers);
echo 'The email ' . (($success) ? 'worked!' : 'failed.') . '
';
If it says it failed there's something wrong with one (or more) of the variables sent to mail().
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
@Main Street James:
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
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?
@obs:
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
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 sendfromemail.php above?
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
in it and load it from your browser.
@obs:
Well that looks wrong
sendmail_path = /usr/local/bin/phpsendmail
try changing it to /usr/sbin/sendmail -t -iTo create a phpinfo page just create a new page and put
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
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:
@obs:
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