如何设置“从”地址在PHP联系表单? [英] How do I set the "From" address in a PHP contact form?

查看:141
本文介绍了如何设置“从”地址在PHP联系表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了PHP联系表单的问题。当我在表单中输入信息并点击提交时,信息将被发送到电子邮件地址,但是没有发送邮件地址。

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.

如何创建一个访客地址,以便用户点击sumbit时,将发送电子邮件作为发件人的电子邮件地址。

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

<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很新,并希望能够得到所有的帮助。

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

提前感谢

推荐答案

你应该这样做:

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

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

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

这篇关于如何设置“从”地址在PHP联系表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆