I am using this code for an email form processor:


Code:

<?PHP
//security function
function IsInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)');
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
     } else {
return false;
     }
}

//Get form entries
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$description = $_POST['job-description'];
$contact_method = $_POST['contact-method'];

//security
if(IsInjected($email)) {
echo "Bad email value!";
exit;
}

//Compose email
$email_from = <valid address>;
$email_subject = "TopPage Design form submission: quotes.html";
$email_body = "*** FORM SUBMISSION *** \n
name: " . $name . "\n
email: " . $email . "\n
phone: " . $phone . "\n
description: " . $description . "\n
contact method: " . $contact_method . "\n
*** END OF EMAIL ***";

//Send email
$to = <valid address>;
$headers = "From" . $email_from . "\r\n";
$headers .= "Reply-To:" . $email . "\r\n";

//send
mail($to, $email_subject, $email_body, $headers);
//success
echo("success.");
?>



But for some reason I don't receive the email, even though i get the success message. Am I doing something wrong?
I suggest enabling error reporting near the top of your script:


Code:
error_reporting(E_ALL);
ini_set('display_errors', true);


The success message never bothers to check the return value from the mail function. I would therefore also suggest checking the return value of mail:


Code:
if (mail($to, $email_subject, $email_body, $headers)) {
    echo 'Success';
} else {
    echo 'Failure';
}


The mail function's behaviour depends on which server it is running on and there can be several limitations (I know one hosting provider refuse to deliver the email unless the "from" or "to" email account is hosted on the domain the mail() function is being used on, for example). Do you know which server it's running on?

Also, which file encoding are you saving the file in? The mail server used on my web host irritatingly turns \n in emails to \r\n, which causes problems if you try to use \r\n in your email (as per the relevant standards) as it turns it into \r\r\n which some email clients and servers refuse to handle. I would recommend changing the email_body bit into something like this:


Code:
$email_body = array();
$email_body[] = '*** FORM SUBMISSION ***';
$email_body[] = 'name: ' . $name;
$email_body[] = 'email: ' . $email;
$email_body[] = 'phone: ' . $phone;
$email_body[] = 'description: ' . $description;
$email_body[] = 'contact method: ' . $contact_method;
$email_body[] = '*** END OF EMAIL ***';
$email_body = implode("\n", $email_body);

Code:

<?PHP
   error_reporting(E_ALL);
   ini_set('display_errors', true);
   //security function
   function IsInjected($str) {
      $injections = array('(\n+)',
         '(\r+)',
         '(\t+)',
         '(%0A+)',
         '(%0D+)',
         '(%08+)',
         '(%09+)');
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str)) {
         return true;
       } else {
         return false;
       }
   }
   
   //Get form entries
   $name = $_POST['name'];
   $email = $_POST['email'];
   $phone = $_POST['phone'];
   $description = $_POST['job-description'];
   $contact_method = $_POST['contact-method'];
   
   //security
   if(IsInjected($email)) {
      echo "Bad email value!";
      exit;
   }
   
   //Compose email
   $email_from = <valid email>;
   $email_subject = "TopPage Design form submission: quotes.html";
   $email_body = "*** FORM SUBMISSION *** \n
   name: " . $name . "\n
   email: " . $email . "\n
   phone: " . $phone . "\n
   description: " . $description . "\n
   contact method: " . $contact_method . "\n
   *** END OF EMAIL ***";
   
   //Send email
   $to = <valid email>;
   $headers = "From" . $email_from . "\r\n";
   $headers .= "Reply-To:" . $email . "\r\n";
   
   //send
   mail($to, $email_subject, $email_body, $headers);
   
   //redirect
   
   if (mail($to, $email_subject, $email_body, $headers)) {
      header('Location: success.php');
   } else {
      echo 'Failure';
   }
?>


It redirects to the success page when this is done... but still, no email.
Remove the redirect for the time being so you can see if any warnings are displayed. In addition, see what I said about the newlines - what happens if you add $email_body = 'Test'; immediately before the mail() call?
Solved. Was using Juju's server and he had mail() setup wrong.

Thanks for all the help though Smile
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement