Code not working?

I am ignorant of PHP and am copying code from a model.

I want my web contact form to email me at [email protected]

Can anyone see why this address code will not work? Any help will be greatly appreciated.

$emailRecipient = “[email protected]”;
$emailHeaders = “From: [email protected]\r\nContent-type: text/html\r\n”;
$emailHeaders .= "Reply-To: " .$_POST[“email”]. “\r\n”;
$emailHeaders .= “\r\n”;

foreach ($_POST as $key => $value) {
$$key = stripslashes($value);
}

Where are you goint to be using the form? i can make you one free of charge. with capcha for your protection

The form (what will go on your html):

First Name *
Last Name *
Email Address *
Telephone Number
Comments *

The PHP Code which captures and Emails your website form

The PHP code below is very basic - it will capture the form fields specified in the HTML form above (first_name, last_name, email, telephone and comments). The fields are then sent off to your email address in plain text.

Note: You need to edit 2 parts of the script below. You need to tell it your email address (this will not be available for anyone to see, it is only used by the server to send your email). You can also specify an email subject line (or just leave the one which is there).
File Name: send_form_email.php (you must use this filename exactly)
Copy and Paste
[php]<?php
if(isset($_POST[‘email’])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "[email protected]";
$email_subject = "Your email subject line";
 
 
function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you 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['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" .
‘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.

<?php } ?>[/php]

That is very kind and generous of you Gonzalez.

Since posting the PHP I have been persevering with tests and have found the error was not in the code but in the email account set-up with an incomplete account description. This resulted in the mail being delivered 1 hr. later and in one case to the wrong mailbox. Should be impossible but it happened due to mailbox ownership being used by the server to resolve the incomplete address. Odd things can happen in cyberspace!

However I would like to keep your code for future educational reference. The main difference between my PHP code and yours is that the PHP in my sample is being used by Action Script 2 and not HTML. Otherwise what you show is very instructive and useful for future reference.

Many Thanks for you help. If I can help you with CS5 or any Mac questions contact me via email and I shall be glad to help if I can.

Best Wishes,
Tony Foster

Sponsor our Newsletter | Privacy Policy | Terms of Service