-1

I have these exact same PHP contact form codes on 2 different servers. 1 is working as intended, but the other is returning the error message instead of the success message. Also, email is not sent to me.

Contact.php codes as follows:

<?php
ob_start();
 if(isset($_POST['submit']))
 {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

 $to="email@gmail.com";
 $subject="Enquiry";
// $headers  = 'MIME-Version: 1.0' . "\r\n";
 //$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .= "From: ".$email_from."\r\n";
 $message="    
Name:
$name      
Email:
$email     
Message:
$query
";
    if(mail($to,$subject,$message,$headers))
        header("Location: http://www.website.com/contact.php?msg=Successful Submission! Thank you for contacting us."); 
    else
        header("Location: http://www.website.com/contact.php?msg=Error Sending Email!");
 }
?>

I'm wondering if it is permissions/server problem? Any help will be greatly appreciated! Thanks!

Melvy
  • 3
  • 7

1 Answers1

0

Your code construct is correct, and the return value (which is your custom error) might be correct "if" the mail function is failing.

A lot of time we find php mail() function is not working. In some cases, the email is not sent even the function returns true. Following are the points you may check while debugging:

  1. First check if the sendmail_path variable is set properly. You can see it from echo – ing phpinfo() in a page. The typical value is /usr/sbin/sendmail -t -i
  2. If you are sending extra headers in the 4th parameter of the function, try to remove it and send. It is a very common scenario to have incorrect header in the mail function.
  3. Check if you have a spam filter installed in your server which is blocking your email.

If you have access check your mail log file to see if the email is blocked by the mail server or have not reached the server yet. If there is no entry of your email in the log, it means you have problem in your php code or php configuration. If it is in the log, you can see why it was not delivered. The typical path to mail log file is /var/log/maillog

Nelson Owalo
  • 2,324
  • 18
  • 37
  • sendmail_path is showing as "no value"... Is that the main problem? – Melvy Jun 08 '15 at 19:19
  • @ Melvallouse `Where the sendmail program can be found, usually /usr/sbin/sendmail or /usr/lib/sendmail. configure does an honest attempt of locating this one for you and set a default, but if it fails, you can set it here.` http://php.net/manual/en/mail.configuration.php . Your mail can never be sent if this isn't set – Nelson Owalo Jun 08 '15 at 19:26
  • Thanks for isolating the problem. I'm not sure where to really begin with the setup. =0 What does sendmail_path = no value mean in the first place? Gosh. Thanks again for the time to answer my questions so far! – Melvy Jun 08 '15 at 20:50
  • To configure that you must have access to the php config file where you can set it manually, look for `sendmail_path = ""` (it might be commented out) and set it to `sendmail_path = "sendmail -t -i"` alternatively, the following full paths might also work `/usr/sbin/sendmail -t -i`, , `/usr/bin/sendmail -t -i`. I recommend you try each and see which works for you. if you dont have access to the php.ini config file, you will have to contact your web host for them to set it for you. – Nelson Owalo Jun 08 '15 at 21:37
  • Ahh.. I've been using my macbook to work on my websites though. Is that the problem? I do have a windows desktop back home so does that mean a switch is somehow needed? – Melvy Jun 09 '15 at 04:32
  • are you using a local server? localhost? – Nelson Owalo Jun 09 '15 at 04:45
  • Might be a silly question but.. How do I check? Basically I'm helping out my friend with a project. He just gave me the control panel access and I've been working with Dreamweaver on a Macbook to upload the website to web server. – Melvy Jun 09 '15 at 04:55
  • Also I'm having trouble finding this php.ini >< Still trying my best to research! – Melvy Jun 09 '15 at 05:01
  • I still can't tell whether you are using a local server(localhost) or if its a live server, (www.mysite.com) from your comment above, so I cant give you correct steps. – Nelson Owalo Jun 09 '15 at 10:04
  • Ah in that case, it's a live server. Sorry wasn't clear. – Melvy Jun 09 '15 at 10:06
  • Just an update, I've been reading up and followed these steps: http://yogeshchaugule.com/blog/2013/configure-sendmail-wamp ......... Not sure I'm heading in the right direction! – Melvy Jun 09 '15 at 10:21
  • I wish we could finish this in the chat section, but I guess thats not a possibility, its getting pretty long. echo this in one of your pages and give me the details, `$_SERVER['REMOTE_ADDR']` I want to check something. – Nelson Owalo Jun 09 '15 at 10:35
  • Sorry I'm not sure how to do that. In any case, does this mean I need to include php.ini and sendmail folder in my hosting site file manager? Like in the root folder or something? – Melvy Jun 09 '15 at 13:32
  • that wont work, I would advise you to ask your webhost to enable this for you instead of looking for hacks. – Nelson Owalo Jun 09 '15 at 20:25
  • 1
    Thanks! Will do just that. Was just wondering if it was something I'm not doing correctly on my end. Will liaise accordingly then! Appreciate the help! :D – Melvy Jun 12 '15 at 05:53
  • @Melvy Great, remember to post back if it works to help others who might stumble upon this. – Nelson Owalo Jun 12 '15 at 07:07