PHP Contact email problem

Messages
1
Reaction score
0
Points
0
Hello, I have made a website from scratch and obtained a php contact form from an online tutorial. I put it in and then uploaded the site here, and it won't send at all. I go to the contact part of my website and send an e-mail, it never sends to the account I tell it to send to in the php coding. Now I know nothing about php, so this is why I got the template from the tutorial. here's the coding, with my email left out, just so I can remain more private.

Code:
<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "my email goes here";
	$email_subject = "Contact Form";
	
	
	function died($error) {
		// your error code can go here
		echo "We are very sorry, but there were error(s) found with the form your 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['name']) ||
		!isset($_POST['email']) ||
		!isset($_POST['comments'])) {
		died('We are sorry, but there appears to be a problem with the form your submitted.');		
	}
	
	$name = $_POST['name']; // required
	$email_from = $_POST['email']; // required
	$comments = $_POST['comments']; // required
	
	$error_message = "";
	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
	$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$name)) {
  	$error_message .= 'The 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 .= "Name: ".clean_string($name)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Comments: ".clean_string($comments)."\n";
	
	
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  ;}
?>
<html>
<head>
<title>Message Sent</title>
</head>
<body>
<center>
Your Message Was Sent! SO IT WORKS!!!
</center>
</body>
</html>

At the end you notice that it will say Message sent, and that shows up everytime, but still no email. Please help.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Some mailers won't relay; that is, they won't send if the "From" address isn't for a domain they service. Some mailers won't accept relayed e-mails. Use an e-mail address of your own for "From" and the user's e-mail just in the "Reply-To" header. If you have control over your domain records, set appropriate SPF records. Don't forget to check your junk e-mail folder.

Check the result of the call to mail—though note that this won't tell you if the e-mail was delivered successfully, just that it was accepted it for delivery. If mail returns false, output a notice that the e-mail couldn't be sent. If you need more information, use a more featureful mail extension or package, such as imap or PEAR's Mail.

Don't use die when outputting HTML. It will result in invalid HTML. Instead, you can use an if statement within the <body> to check whether there is an error message to output.

The POSIX regex functions are deprecated. Use PCRE (or the filter functions) instead.

<br/> and <center> aren't semantic. Use structurally appropriate elements for HTML and use CSS for presentation.

Use
PHP:
 or [html] BBCode tags rather than [code] to mark-up PHP or HTML, respectively.

[php]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
>
<html>
  <head>
    <title>Sending Message</title>
  </head>
  <body>
    <?php
      if(isset($_POST['email'])) {
          # edit the 3 variables below as required
          $from="e-mail address in server's domain";
          $recipient = 'my email goes here';
          $subject = empty($_POST['subject']) 
              ? 'Contact Form' // edit here
              # could also remove matching characters
              : preg_replace_callback('/[^\p{L} -\[\]-~]+/u', 'urlencode', trim($_POST['subject']));

          $fields = array('name' => array(
                              'filter' => FILTER_VALIDATE_REGEXP,
                              'options' => array('regexp' => "/^[\p{L} .,'-]+\$/i"),
                              'description' => "Valid characters are letters, periods, spaces, dashes, apostrophes, commas."), 
), 
                          'email' => FILTER_VALIDATE_EMAIL, 
                          'comments' => array(
                              'filter' => FILTER_VALIDATE_REGEXP,
                              'options' => array('regexp' => '/(\S{3}\s+){3}/'),
                              'description' => 'Must contain at least three words at least three letters long.')
                              # or: 'description' => 'Must be longer.'
              );
      
          # filter ensures name & email are safe from injection
          $data = filter_var_array(array_map('trim', $_POST), $fields);
          $message[] = "Form details below.\n";
          foreach ($data as $field => $value) {
              if (is_null($value)) {
                  $errors[$field] = "Field '$field' is empty.";
              } elseif ($value === False) {
                  $value = htmlspecialchars($_POST[$field]);
                  $message = "The value '$value' for field '$field' is invalid.";
                  if (isset($fields[$name]['description'])) {
                      $message .= ' ' . $fields[$name]['description'];
                  }
                  $errors[$field] = $message;
              } else {
                  $message[] = ucfirst($field) . ': ' . $value;
              }
          }

          if ($errors) {
              if (count($errors) > 1) {
                  $plural = 's';
              } else {
                  $plural = '';
              }
              ?>
              <p>We are very sorry, but your submission had the following error<?php echo $plural ?>:
                <ul>
                  <?php foreach ($errors as $error) { ?>
                    <li><?php echo $error; ?></li>
                  <?php } ?>
                </ul>
              Please go back and fix <?php echo ($plural ? 'them' : 'it') ?>.
              </p>
              <?php
          } else {
              // create email headers
              $headers = "From: $from\r\nReply-To: $data[email]\r\nContent-type: text/plain\r\nX-Mailer: PHP/" . phpversion();
              $message = implode("\n", $message);

              if (mail($recipient, $subject, $message, $headers);) {
                  ?><p>Your message was accepted for delivery.</p><?php
              } else {
                  ?><p>Your message wasn't accepted by the e-mail server.</p><?php
              }
          }
      }
    ?>
  </body>
</html>
 
Top