Jump to content

Recommended Posts

Hi guys,

 

Hope you can help. I've moved a word press site from one cPanel host to another. Since doing so the contact form doesn't work. The PHP versions are the same, with similar modules and php settings.

 

The issue is, the form appears to send properly from the site, but the email never actually arrives. I tried changing the email its sending to, to a gmail account, a hotmail etc.

 

To make sure the server use the mail() function i did the below and received the email straight away:

 

<?php 
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );
    $from = "bob@test.com";
    $to = "blah@example.co.uk";
    $subject = "PHP Mail Test script";
    $message = "This is a test to check the PHP Mail functionality";
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers);
    echo "Test email sent";
?>

Here is the code to process the contact form, I've changed the email address for obvious reasons! Also, the eregi function is deprecated but not sure how to replace it with the new email validation function, if someone could help with that too that'd be awesome. I removed the eregi check to ensure that wasn't causing the issue just FYI.

 

<?php
if(trim($_POST['checking']) !== ''){
  $capchaError = true;
} else {
  if(trim($_POST["name"]) === ''){
    $formError = true;
  } else {
    $name = trim($_POST['name']);
  }
  if(trim($_POST["email"]) === '') {
    $formError = true;
  } elseif (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST["email"]))) {
    $formError = true;
  } else {
    $email = trim($_POST['email']);
  }
  if(trim($_POST["number"]) === '') {
    $formError = true;
  } else { // validate phone to be either 10 or 11 digits
    $number = trim($_POST["number"]);
    $pattern = "/(((\+44)? ?(\(0\))? ?)|(0))( ?[0-9]{3,4}){3}/"; // allows for numbers 8 - 12 digits in length, starting 0 or +44
    $match = preg_match($pattern,$number);
    if ($match === 0) { // if validation fails
      $formError = true;
    } else { // if valiudation passes
      $phone = trim($_POST["number"]);
    }
  }
  if(trim($_POST["subject"]) === ''){
    $formError = true;
  } else {
    $msgSubject = trim($_POST['subject']);
  }
  if(trim($_POST["message"]) === '') {
    $formError = true;
  } else {
    if(function_exists("stripslashes")) {
      $comments = stripslashes(trim($_POST['message']));
    } else {
      $comments = trim($_POST['message']);
    }
  }
  if($formError !== true) {
    $email_to = "removed@gmail.com";
    $subject = "Message from the website";
    $message = 'Name: '. $name . "\n\nPhone: " . $phone . "\n\nEmail: " . $email . "\n\nSubject: " . $msgSubject. "\n\nMessage: " . $comments;
    $headers = 'From: '.$name.' <'.$email.'>';
    mail($email_to, $subject, $message, $headers);
    $sent = true;
  }
  if(isset($sent) && $sent == true) {
    header("Location: /thank-you/");
  } else {
    header("Location: /message-not-sent/");
  }
}
if($capchaError == true) {
  header("Location: /message-not-sent/");
}
?>

The code for the form on the contact page is as follows (some details changed again):

 

  <form action="http://www.site.co.uk/wp-content/themes/site/php/contactprocess.php" id="contact" method="post" class="threeQuarter">
                            <input type="text" id="name" name="name" placeholder="Name" required>
                            <input type="email" id="email" placeholder="Email" name="email" required>
                            <input type="tel" id="number" placeholder="Number" name="number" required>
                            <input type="text" id="subject" placeholder="Subject" name="subject" required>
                            <textarea id="message" placeholder="Message" name="message" required></textarea>
                            <span id="screenReader">Spam Protection - <i>Do not enter anything in this field</i>
                                <input type="text" name="checking" class="checking" placeholder="Ignore" value="" />
                            </span>


                            <button class="button fifth" name="serviceFormOne" type="submit">Send</button>
                        </form>

 

Thanks for any help.

 

There are two things you need to change:

 

$headers = 'From: '.$name.' <'.$email.'>';
// Change to
$headers = 'Reply-To: '.$name.' <'.$email.'>';

Your server is (most likely) not authorized to send mail "From" whatever is in $email. Sending mail from "yourDomain.com" and saying it is from "someOtherDomain.com" will appear as a forgery and likely get you SPAM points (in this case the email with the fewest points, wins). Mail from your server needs to say it is from your server, and the server/PHP should be configured that way. The "Reply-To" header will allow the recipient to reply to the $email, which is probably what you are trying to accomplish there.

 

mail($email_to, $subject, $message, $headers);
$sent = true;

// Change to

$sent = mail($email_to, $subject, $message, $headers);

You are arbitrarily saying the email was sent. mail() will return false if it cannot submit it to the sendmail system. That would be a server configuration issue. However, just because you get true from mail() does not mean the mail went out. It just means it was handed to the server's sendmail system. So, if this returns false, check the server's configuration. If it returns true, and you still don't get it, look at the sendmail configuration and (to some extent) the PHP configuration.

 

Always check your Junk mail/Spam filters when looking for mail from PHP. It is possible there are headers missing or present that will get the mail flagged or discarded.

  • Like 1
  • 1 month later...

Thanks for the reply. So I tried those two changes, changing the header didn't seem to resolve it, and replacing mail as you suggested seemed to trigger the last part of the code, as it redirected me to the page saying it hadn't sent.

 

Its interesting, as the mail function works using the example provided in my first post, so it doesn't seem to be the web server rejecting the messages (but I understand what you are thinking).

 

It must be the poorly written code (which I inherited by the way!). I am really out of touch with coding these days. Anyone have any other ideas?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.