2010-02-09 89 views
2

我遇到了我創建的PHP聯繫表單的問題。當我在表單中輸入信息並單擊提交時,信息將發送到電子郵件地址,但沒有附加到電子郵件的發件人地址。如何在PHP聯繫表單中設置「發件人」地址?

如何創建一個seneders地址,以便當用戶點擊sumbit時,它將發送電子郵件與他的電子郵件地址作爲發件人。

我的表單的代碼如下所示:

<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"> 

<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> 

我很新的PHP,並感謝所有幫助我可以用這個搞定。

在此先感謝

+3

閱讀有關郵件功能additional_headers的文檔 – meouw 2010-02-09 13:49:19

回答

1

你應該做這樣的事情:

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

該行開始$sanitizedEmail確保用戶不能從您的形式發送垃圾短信。

8

您可以在郵件()調用作爲第四個參數(目前你只添加了$ visitoremail。頭需要由被分隔的「添加多個額外的頭\ r \ n」。下面是來自PHP.net/manual如何做其他標頭的摘錄:

// 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); 
2

您可以添加‘從:’標頭,當你爲了設置發件人的地址調用mail()

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

您還可以追加「回覆:」頭,如果你想(或者你想包括報頭的任何其他數),

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

另一種選擇是設置從默認解決你的php.ini文件,或將其設置在這樣的劇本,

ini_set(sendmail_from, "[email protected]_here.com"); 

PHP Manual page for mail()有更多的信息。

1

其他答案解釋如何添加From頭。如果您不仔細檢查腳本輸入,則存在一個重要的可能安全漏洞。你的表格可能變成一個垃圾郵件機器,可能會導致你的各種悲傷。

閱讀上的電子郵件標題注射: http://www.damonkohler.com/2008/12/email-injection.html

驗證電子郵件地址的電子郵件地址和消毒所有輸入。

相關問題