tags:

views:

95

answers:

7

Hi

I have a problem with the PHP contact form I have created. When I enter information into the form and click submit the information is sent to an email address but there is no senders address attached to the email.

How do I create a seneders address so that when the user clicks sumbit it will send the email with thier email address as the sender.

The code for my form is shown below:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email Form </title>
</head>
<body>

<form method="post" action="sendeail.php">

<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>

<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />


---------------(Sending Scripit below)-----------------------------

Name: <br />
<input type="text" name="visitor" size="35" />
<br />
Address:<br />
<input type="text" name="visitoradd" size="35" />
<br />

City<br />
<input type="text" name="visitorcity" size="35" />
<br />

Postcode<br />
<input type="text" name="visitorpost" size="35" />
<br />

Email:<br />
<input type="text" name="visitormail" size="35" />
<br />

Telephone Number:<br />
<input type="text" name="visitortel" size="35" />
<br />
Bookkeeping / Payroll :<br />
<select name="bp" size="1">
<option value=" Bookkeeping">Bookkeeping </option>
<option value=" Payroll ">Payroll</option>
</select>
<br />
<br />
Number of transactions : <br />
<input type="text" name="transcations" size="35" />
<br />
Number of employees <br />
<input type="text" name="employees" size="35" />
<br />
<br />
Payroll weekly / monthly:<br />
<select name="pmw" size="1">
<option value=" Weekly">Weekly</option>
<option value=" Monthly">Monthly</option>
</select>
<br />
<br />
<input type="submit" value="Submit" />
<br />
</form>
</body>
</html>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sendemail Script</title>
</head>
<body>
<?php

$visitor = $_POST['visitor'];
$visitoradd = $_POST['visitoradd'];
$visitorcity= $_POST['visitorcity'];
$visitorpost= $_POST['visitorpost'];
$visitormail = $_POST['visitormail'];
$visitortel = $_POST['visitortel'];
$bp = $_POST['bp'];
$transcations = $_POST['transcations'];
$employees = $_POST['employees'];
$pmw = $_POST['pmw'];


if(!$visitormail == "" && (!strstr($visitormail,"@") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Information was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}

if(empty($visitor) || empty($visitormail)  || empty($visitorcity) || empty($visitorpost) || empty($visitoradd)   || empty($visitortel) || empty($bp) || empty($transcations)  || empty($employees) || empty($pmw) || empty($pmw)) 
{
echo "<h2>Use Back - fill in all fields</h2>\n";
die ("Use back! ! ");
}

$message = "
Name: $visitor\n
Address: $visitoradd\n
City: $visitorcity\n
Post Code: $visitorpost\n
Email: $visitormail\n
Phone Number: $visitortel\n
Bookkeeping/Payroll: $bp\n
Number of Transactions: $transcations\n
Number Of Employees: $employees\n
Payroll Weekly/Monthly: $pmw\n"
;

$subject = "Payment Details";

mail("[email protected]",$subject,$message,$visitormail);

?>

<p align="center">
<br />
Thank You : <?php echo $visitor ?> ( <?php echo $visitormail ?> )
<br />

<br />
<a href="contact3.php">Click here to Finish</a>
</p> 

</body>
</html>

I am quite new to PHP and would appreciate all the help I can get with this.

Thanks in advance

+5  A: 

mail("[email protected]",$subject,$message, 'From: ' . $visitormail);

Leventix
+7  A: 

You can add multiple extra headers in the mail() call as the fourth argument (currently you are only adding the $visitoremail. headers need to be delimited by a "\r\n". Here's an excerpt from the PHP.net/manual on how to do additional headers:

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
vicatcu
+2  A: 

You can add the "From:" header when you call mail() in order to set the sender's address.

$headers = "From: ".$visitor."<".$visitormail.">\r\n";    
mail("[email protected]", $subject, $message, $headers);

You could also append the "Reply-To:" header if you want (or any other number of headers you want to include),

$headers .= "Reply-To: ".$visitor."<".$visitormail.">\r\n";

The other option is to set a default from address in your php.ini file, or set it in the script like this,

ini_set(sendmail_from, "your_from@address_here.com"); 

The PHP Manual page for mail() has more information.

Rich Adams
+1  A: 

You should do something like this:

$sanitizedEmail = filter_var($visitormail, FILTER_SANITIZE_EMAIL);
mail('[email protected]', $subject, $message, 'From: ' . $sanitizedEmail);

The line beginning $sanitizedEmail ensures that a user cannot send spam messages from your form.

Rowan
+1  A: 

Other answers explain how to add the From header. There is an important possible security vulnerability if you do not carefully check the script input. Your form can be turned into a spam machine that can cause you all sorts of grief.

Read up on email header injection: http://www.damonkohler.com/2008/12/email-injection.html

Verify that the email address is an email address and sanitize all input.

Scott Saunders
A: 

Have a look at PHPMailer

You can do almost everything using PHPMailer. Click here to visit the site and to obtain a copy

http://phpmailer.worxware.com/index.php?pg=phpmailer

PHPMailer does all the logic for you.

Eg. $mailer->addAddress('[email protected]');

Roland
A: 

Great info guys

But I want the "From:" to be generated by the persons email address who is filling out the form. So each user that fills out the contact form will have to provide their email address which will arrive in my inbox as a message from that user with their email address and not the server

Hope you can help

Thanks

So what you want is exactly what Leventix and Rich Adams (in his first code snippet) answered yesterday ... Make sure you sanitize the email address as well before using it (using Rowan's code snippet).
wimvds
Thanks a lot guys used Rich adams code and that worked fine
Also thanks Rowan for the Spam tip very handy