如何在PHP联系表单中设置“发件人”地址?

时间:2010-02-09 13:43:17

标签: php email forms

我在创建的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("sd07sb@leeds.ac.uk",$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的新手,非常感谢我能得到的所有帮助。

提前致谢

6 个答案:

答案 0 :(得分:8)

您可以在mail()调用中添加多个额外标头作为第四个参数(目前您只添加$ visitoremail。标头需要用“\ r \ n”分隔。这是PHP的摘录。关于如何进行额外标题的网/手册:

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

答案 1 :(得分:5)

mail("sd07sb@leeds.ac.uk",$subject,$message, 'From: ' . $visitormail);

答案 2 :(得分:2)

您可以在致电mail()时添加“发件人:”标题,以便设置发件人的地址。

$headers = "From: ".$visitor."<".$visitormail.">\r\n";    
mail("sd07sb@leeds.ac.uk", $subject, $message, $headers);

如果您愿意(或要包含任何其他数量的标题),您还可以附加“回复:”标题,

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

另一个选项是在php.ini文件中设置默认地址,或者在脚本中设置,如下所示,

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

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

答案 3 :(得分:1)

你应该这样做:

$sanitizedEmail = filter_var($visitormail, FILTER_SANITIZE_EMAIL);
mail('recipient@email.com', $subject, $message, 'From: ' . $sanitizedEmail);

$sanitizedEmail开头的行确保用户无法从您的表单发送垃圾邮件。

答案 4 :(得分:1)

其他答案解释了如何添加From标头。如果不仔细检查脚本输入,则存在一个重要的安全漏洞。您的表单可以变成垃圾邮件机器,可能会引起各种各样的悲痛。

阅读电子邮件标题注入: http://www.damonkohler.com/2008/12/email-injection.html

验证电子邮件地址是否为电子邮件地址并清理所有输入。

答案 5 :(得分:0)

看看PHPMailer

你可以使用PHPMailer做几乎所有事情。点击此处访问该网站并获取副本

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

PHPMailer为您完成所有逻辑。

EG。 $ mailer-&GT; addAddress('sd07sb@leeds.ac.uk');