PDA

View Full Version : Contact From PHP problem


tahsin1982
04-14-2012, 03:41 AM
Hi guys,

Sorry if this has been asked before, I am not really that good in PHP and was building a contact for with this scrip I found on the net

<?php

// Set email variables
$email_to = 'xxxxx@xxxx.com';
$email_subject = 'Form submission';

// Set required fields
$required_fields = array('fullname','email','comment');

// set error messages
$error_messages = array(
'fullname' => 'Please enter a Name to proceed.',
'email' => 'Please enter a valid Email Address.',
'comment' => 'Please enter your Message.'
);

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));


// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);

// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);

// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}

// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'New Website Comment: ' . "\n\n";

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}

// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);

// Update form switch
$form_complete = TRUE;
}
}

function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}

function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}

?>The script is great and works perfect on the website but the problem I notice is when I test the form and press submit and check it on my inbox.

The Receiver address shows my email address with my webhost address (not the urname@domain.com) rather than the original senders add. Only if I open the message I can see the senders email address in the email field but thats about it.

I am not sure how to fix this but if anyone has any tips would be very helpful as I really like this script.

cheers

edbr
04-14-2012, 04:11 AM
it shows that because thats where its being sent from. perhaps you could add it to the subject line if you wanted,

to do that move $email_subject = 'Form submission';

to

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}

$email_subject = 'Form submission from ' .$_POST['email'];

// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);

not tested but should be ok

tahsin1982
04-14-2012, 04:50 AM
Hi thanks for replying I tried it but didnt work. Still showing the senders email add as my own hosting email add. The subject is displaying fine its just the senders name dosent show up in the box but my domain email add. I am stumped...

tahsin1982
04-14-2012, 09:34 AM
Ok this is getting confusing and fraustrating. I think my cache in my server or something is not really updating changes I make to the php script. Even when I change the
// Set email variables
$email_to = 'xxxxx@xxxx.com';
$email_subject = 'Form submission';

example to a different email address. Its still sending email address to the old one and nothing to my new one.

I even redownloaded the updated php files to make sure whether the codes are updated and they are but somemore i think cache or something is messed.

I better email my hosting provider about this.

tahsin1982
04-14-2012, 04:54 PM
I got it fixed :D

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}

$email_subject = 'Form submission from ' .$_POST['email'];

$email_headers = "From: " . $_POST['email'] . "\n" . "Return-Path: " . $_POST['email'] . "\n" . "Reply-To: " . $_POST['email'] . "\n";

// if validation passed ok then send the email


mail($email_to, $email_subject, $email_content, $email_headers);

The header was the part I needed to declare so now its working thanks again ED for the telling where to put the code.

cheers