Help please

Hi, I’m a complete beginner to PHP and need some help and explanation as to how I can get the attached to arrive into my mailbox marked with the sender’s name rather than my email address. Many thanks in advance for any help.

[php]<?
session_start();

if ($_SERVER[“REQUEST_METHOD”] <> “POST”)
die(“You can only reach this page by posting from the html form”);

if ($_POST[“captcha_input”] == $_SESSION[“pass”])
{
header(“Location: thank_you.html”);

//sends email via php to the following address
$mailuser = "[email protected]";

//echo 'default chosen address: '.$mailuser;

$header = "Return-Path: ".$mailuser."\r\n"; 
$header .= "From: $myemail\n <".$mailuser.">\r\n"; 
$header .= "Content-Type: text/html;"; 
	  
$mail_body = '
Name: '. $_POST[name] . '<br><br>
Email: '. $_POST[email] . '<br><br>
Telephone: '. $_POST[telephone] . '<br><br>
Message: '. $_POST[message] . '<br><br>'
;	
mail ($mailuser, 'Contact Form', $mail_body, $header);

} else {
header(“Location: error.html”);

}
?>[/php]

Look at this section of the code:

[php]$header .= “From: $myemail\n <”.$mailuser.">\r\n"; [/php]

That is the FROM section so change it to something like:

[php]$header .= “From: $sender\r\n”; [/php]

And set $sender to the users name.

Many thanks Scott - that’s done just the job. Happy as a pig in shhhhhhh now :smiley:

Just another heads up, it would be better practice to just post the variables into the email rather than post it into a variable and use that.

E.g.

[php]//sends email via php to the following address
$mailuser = "[email protected]";

//echo 'default chosen address: '.$mailuser;

$header = "Return-Path: ".$mailuser."\r\n"; [/php]

Would be better as:

[php]//sends email via php to the following address

//echo ‘default chosen address: [email protected]’;

$header = "Return-Path: [email protected] \r\n"; [/php]

Or if you were posting the name in:

[php]//sends email via php to the following address

//echo ‘default chosen address: [email protected]’;

$header = "Return-Path: ".$_POST['defaultaddress']." \r\n"; [/php]

As it uses less of the servers CPU. Albeit only very slightly less, but the more resources you save, the more efficient your website/server becomes.

Thanks again. I did wonder why it was written like that and thought it looked a long way for a short cut. My admin colleague is just delighted that every form within our website doesn’t come in from himself now. I’ll make that wee change. Cheers.

Sponsor our Newsletter | Privacy Policy | Terms of Service