Jump to content

Email address not showing


JulianWill

Recommended Posts

Ok I am very new to php so please excuse my lack of knowledge.

 

I have a simple contact form that I have placed onto one of my sites. The form works correctly in that it get sent to the correct address without any problems.

 

The issue I am having is with the 'From' header. It will not show the senders email address as I would like. Instead it seems to be showing the server address.

 

I have been 'playing' with the code for 2 days now and I still cannot get it to work.

 

Any suggestions would be greatfully received.

 

Regards.

 

<?php



if(!$_POST) exit;

$email = $_POST['Email'];
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$comments = $_POST['Comments'];






//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('Name','Email','Telephone','Comments');
$required = array('Name','Email','Telephone','Comments');


$your_email = "**********************";
$email_name = "Message From your Website:";


foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
	  if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(@mail($your_email,$email_name,$email_content)) {
	echo 'Thank you for your message, we will be in touch shortly'; 
} else {
	echo 'ERROR!';
}
}

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();

@mail($your_email,$name,$email,$telephone,$comments,$headers);

?>

Link to comment
Share on other sites

@mail($your_email,$name,$email,$telephone,$comments,$headers);

 

Have a look at the mail function in the manual. I think you're a bit confused on the parameters it can take.

 

This is from the manual

 

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

 

It will most always be

 

mail($emailtosendto, $subjectofemail, $emailmessagebody, $headers) 

 

It looks like you're just adding a comma and any field you want sent as parameters, mail() expects very specific things

 

 

Link to comment
Share on other sites

Matthew

 

Thats very helpful.

 

I have amended the code as follows however, now I get two emails sent. One in the correct format that I required (with the email address showing) and another with the server address showing.

 

How can I stop this second mail being sent.

 

<?php



if(!$_POST) exit;

$email = $_POST['Email'];
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$comments = $_POST['Comments'];






//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}
if($errors==1) echo $error;
else{
$values = array ('Name','Email','Telephone','Comments');
$required = array('Name','Email','Telephone','Comments');


$your_email = "*****";
$email_subject = "Message From the HDS Builders Website:";


foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
	  if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

if(@mail($your_email,$email_name,$email_content)) {
	echo 'Thank you for your message, we will be in touch shortly'; 
} else {
	echo 'ERROR!';
}
}

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();

mail($your_email, $email_subject, $email_content, $headers)

?>

 

Link to comment
Share on other sites

You're sending it twice

 

here:

 

if(@mail($your_email,$email_name,$email_content)) {
	echo 'Thank you for your message, we will be in touch shortly'; 
} else {
	echo 'ERROR!';
}

 

and

 

here:

 

mail($your_email, $email_subject, $email_content, $headers)

 

The first one is right above the other, and the second one is the correct one :)

 

 

Link to comment
Share on other sites

Thanks for the promt reply.

 

I have tried deleting this line:

 

if(@mail($your_email,$email_name,$email_content, $headers)) {

 

However when I do that I get this error message:

 

Parse error: syntax error, unexpected T_ELSE in /home/rcttest/public_html/contact.php on line 88

 

Which is reffering to this line:

 

} else {

 

I cannot work out why this is.

 

Regards.

Link to comment
Share on other sites

<?php

if(!$_POST) exit;

$email = $_POST['Email'];
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$comments = $_POST['Comments'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}

if($errors==1) echo $error;
else{
$values = array ('Name','Email','Telephone','Comments');
$required = array('Name','Email','Telephone','Comments');


$your_email = "*****";
$email_subject = "Message From the HDS Builders Website:";


foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
	  if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }
}

}

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();

mail($your_email, $email_subject, $email_content, $headers)

?>

 

Give that a try

 

 

Link to comment
Share on other sites

Note that the code is missing a semi-colon; the last line should be:

 

mail($your_email, $email_subject, $email_content, $headers);

 

 

Also, the if/else version that you had before is better if you're looking to catch errors:

 

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();	
if(@mail($your_email, $email_subject, $email_content, $headers)) {
    echo 'Thank you for your message, we will be in touch shortly';
} else {
    echo 'ERROR!';
}

Link to comment
Share on other sites

Cyber

 

Thanks for your replies. When I use the if/else version I am still getting error messages.

 

The version that Matthew has pasted works well but without my 'Thank you' message.

 

Do you know what would be the best way to impliment this?

 

Regards

 

Julian

Link to comment
Share on other sites

I am using outlook 2010. I have just sent another two and they went to the inbox so hopefully they will keep doing that.

 

In sending those messages I have noticed another problem. If the user does not complete their email address correctly then they get taken to the error message that looks like this:

 

Invalid email address enteredERROR!

 

How can I change the code to get rid of the

ERROR!
at the end.

 

Thanks

 

 

Link to comment
Share on other sites

It looks like the problem is that your mail() function is outside of your if/else statement. If you move it back in there, the error text should go away.

 

if($errors==1) echo $error;
else{
     ...

     $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();
     if(@mail($your_email, $email_subject, $email_content, $headers)) {
          echo 'Thank you for your message, we will be in touch shortly';
     } else {
          echo 'ERROR!';
     }
}
?>

Link to comment
Share on other sites

Could you show me exactly where I shold be putting the mail() function. I have moved it to where I think it should go and it does solve the email error message issue however it then creates another with the 'Please go back and complete all fields, thank you' message.

 

This message now appears like this:

 

ERROR!Please go back and complete all fields, thank you.

 

Thanks

Link to comment
Share on other sites

You'll need to add it after your foreach loop:

 

<?php
...
if($errors==1) echo $error;
else{
    ...
     foreach($values as $key => $value){
          if(in_array($value,$required)){
               if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
                    if( empty($_POST[$value]) ) {
                         echo 'Please go back and complete all fields, thank you.';
                         exit;
                    }
               }
               $email_content .= $value.': '.$_POST[$value]."\n";
          }
     }

     $headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();
     if(@mail($your_email, $email_subject, $email_content, $headers)) {
          echo 'Thank you for your message, we will be in touch shortly';
     } else {
          echo 'ERROR!';
     }
}
?>

 

 

Basically, you only want to send out a message if there wasn't any errors found in the foreach loop. And since the loop is set to exit if any errors are found, you shouldn't see anything after an error was found.

Link to comment
Share on other sites

Thanks again for the reply.

 

I have the code as follows:

 

 <?php



if(!$_POST) exit;

$email = $_POST['Email'];
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$comments = $_POST['Comments'];






//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}


if($errors==1) echo $error;
else{
$values = array ('Name','Email','Telephone','Comments');
$required = array('Name','Email','Telephone','Comments');


$your_email = "*********.co.uk";
$email_subject = "Message From the  Website:";




foreach($values as $key => $value){
  if(in_array($value,$required)){
	if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
	  if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; }
	}
	$email_content .= $value.': '.$_POST[$value]."\n";
  }

}
}

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();

if(@mail($your_email, $email_subject, $email_content, $headers)) {
echo 'Thank you for your message, we will be in touch shortly'; 
} else {echo 'ERROR!';
}



?>

 

Which I thought was correct however it is still showing the work 'ERROR' with the invalid email message.

 

 

Link to comment
Share on other sites

It needs to go below your foreach loop, but inside the else statement which tests the validity of the e-mail:

 

if($errors==1) echo $error;
else{
     ...
     foreach...

     //<-- mail() function goes here
}

 

 

Here is the exact code:

 

<?php
if(!$_POST) exit;
$email = $_POST['Email'];
$name = $_POST['Name'];
$telephone = $_POST['Telephone'];
$comments = $_POST['Comments'];

//$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){
$error.="Invalid email address entered";
$errors=1;
}

if($errors==1) echo $error;
else{
$values = array ('Name','Email','Telephone','Comments');
$required = array('Name','Email','Telephone','Comments');

$your_email = "*********.co.uk";
$email_subject = "Message From the  Website:";

foreach($values as $key => $value){
	if(in_array($value,$required)){
		if ($key != 'Name' && $key != 'Comments' && $key != 'Telephone') {
			if( empty($_POST[$value]) ) { echo 'Please go back and complete all fields, thank you.'; exit; }
		}
		$email_content .= $value.': '.$_POST[$value]."\n";
	}
}

$headers = "From: $email\r\n"."Reply-To: $email\r\n".'X-Mailer: PHP/' . phpversion();
if(@mail($your_email, $email_subject, $email_content, $headers)) {
	echo 'Thank you for your message, we will be in touch shortly';
} else {
	echo 'ERROR!';
}
}
?>

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • 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.