我制作了一个php表单,但我无法确定如何让它自动回复填写的电子邮件。 我想向刚刚使用我们拥有的表单的人发送一个confurmation,但我不知道如何制作它。
这是我的代码示例。 我想发送自定义邮件到$ email我该怎么做?!
我做了一些更改,但我没有收到电子邮件喷气机。 Maby我确实理解你了,但你可以再次检查一下吗。
<?php
/* Set e-mail recipient */
$myemail = "opgeven@kidsnthingspernis.nl";
/* Check all form inputs using check_input function */
$subject = check_input($_POST['subject']);
$Voornaam = check_input($_POST['Voornaam'], "Vul A.U.B. uw Voornaam in.");
$Achternaam = check_input($_POST['Achternaam'], "Vul A.U.B. uw Achternaam in.");
$VoornaamKind = check_input($_POST['VoornaamKind'], "Vul A.U.B. de Voornaam van uw in.");
$AchternaamKind = check_input($_POST['AchternaamKind'], "Vul A.U.B. de Achternaam van uw in.");
$email = check_input($_POST['email'], "Vul A.U.B. uw Email adres in.");
$Leeftijd = check_input($_POST['Leeftijd'], "Vul A.U.B. de leeftijd van uw kind in.");
$Groep = check_input($_POST['Groep'], "Vul A.U.B. de basisschoolgroep van uw in.");
$Opmerking= ($_POST['Opmerking']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail adres klopt niet");
}
/* Let's prepare the message for the e-mail */
$message = "Hallo!
Je contact formulier is ingevuld door:
Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam
Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind
E-mail: $email
Groep: $Groep
Leeftijd: $Leeftijd
Opmerking:
$Opmerking
Einde bericht.
"
;
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: bedankt.html');
exit();
/* Functions we used */
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if (strlen($data) == 0)
{
return false;
}
else
{
return $data;
}
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
$email = 'email';
$subject = 'subject';
$message = "Hallo!
U/jij bent nu opgegeven voor Kids N Theatre met De Vliegende Speeldoos.
Dit is wat wij aan gegevens hebben gekregen:
Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam
Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind
E-mail: $email
Groep: $Groep
Leeftijd: $Leeftijd
Opmerking:
$Opmerking
Einde bericht.
";
$headers = 'From: opgeven@kidsnthingspernis.nl' . "\r\n" .
'Reply-To: opgeven@kidsnthingspernis.nl' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email, $subject, $message, $headers);
?>
<?php
exit();
}
?>
答案 0 :(得分:0)
您设置此脚本的方式存在多个错误,让我尝试将它们全部指向您。
首先,您正在使用header('Location...
,无论是否有任何show_error
函数实际上正在其上方回显/打印,这将导致错误...
警告:无法修改标头信息 - 已由
发送的标头
这是因为您不允许在标题之前输出任何内容(请在PHP documentation of header中详细了解此内容。)
为了避免这种情况,你必须以某种方式告诉你的脚本存在错误,并检查它,然后只有在没有错误的情况下才允许执行header
。
在您的情况下,我会从$problem
函数中删除check_input
变量,并将其转换为错误消息数组。这样,如果有多个表单输入未正确填写,您就可以输出多个错误消息:
$problem = array();
如果验证失败,我会将check_input
函数修改为return false
:
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if (strlen($data) == 0)
{
return false;
}
else
{
return $data;
}
}
更进一步,根据变化,我将通过以下方式使用此功能:
$Leeftijd = check_input($_POST['Leeftijd']);
if (!$Leeftijd)
{
$problem[] = "Vul A.U.B. uw Leeftijd in.";
// Square brackets ('[]') are one of the ways to add an element to an array
}
我也避免以这种方式使用show_error
,因为它可能会导致header
出现同样的错误,因为您可以简单明了地使用该功能在其上方输出html代码。
因此,我只会在检查$problem
数组时使用它,并且只有在有错误时才使用它,否则允许发送电子邮件。为了使它正常工作,你还需要对它进行一些修改:
function show_error($myError)
{ ?>
<html>
<body>
<b>Please correct the following error(s):</b><br />
<?php
foreach($myError as $error)
// the `foreach` function takes care of looping through the array
{
echo $myError."<br />";
// adding a line break to be able to show multiple errors
}
?>
</body>
</html>
<?php } ?>
然后我将通过以下方式使用最终检查和上述修改函数:
if (count($problem) > 0)
{
show_error($problem);
}
else
{
mail($myemail, $subject, $message);
}
我知道有很多其他方法可以做到这一点,但我发现这是通过提供最理想的结果之一来修改现有脚本的方法。
注意:我知道花括号({
和}
)并不总是必要的,但最好是以newby开头。
答案 1 :(得分:0)
这是一个工作脚本!! 其实真的很容易。 感谢您的帮助!
<?php
/* Set e-mail recipient */
$myemail = 'opgeven@kidsnthingspernis.nl' . ', '; // note the comma
$email .= check_input($_POST['email']);
/* Check all form inputs using check_input function */
$subject = check_input($_POST['subject']);
$Voornaam = check_input($_POST['Voornaam'], "Vul A.U.B. uw Voornaam in.");
$Achternaam = check_input($_POST['Achternaam'], "Vul A.U.B. uw Achternaam in.");
$VoornaamKind = check_input($_POST['VoornaamKind'], "Vul A.U.B. de Voornaam van uw in.");
$AchternaamKind = check_input($_POST['AchternaamKind'], "Vul A.U.B. de Achternaam van uw in.");
$email = check_input($_POST['email'], "Vul A.U.B. uw Email adres in.");
$Leeftijd = check_input($_POST['Leeftijd'], "Vul A.U.B. de leeftijd van uw kind in.");
$Groep = check_input($_POST['Groep'], "Vul A.U.B. de basisschoolgroep van uw in.");
$Opmerking= ($_POST['Opmerking']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail adres klopt niet");
}
/* Let's prepare the message for the e-mail */
$message = "Hallo!
Yes er is weer iemand opgegeven voor Kids 'N Things met De Vliegende Speeldoos.
Dit zijn de gegevens:
Ouder/Verzorger
Voornaam: $Voornaam
Achternaam: $Achternaam
Kind:
Voornaam Kind: $VoornaamKind
Achternaam Kind: $AchternaamKind
E-mail: $email
Groep: $Groep
Leeftijd: $Leeftijd
Opmerking:
$Opmerking
Einde bericht.
"
;
/* Send the message using mail() function */
mail($myemail, $subject, $message);
mail($email, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: bedankt.html');
exit();
/* Functions we used */
function check_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if (strlen($data) == 0)
{
return false;
}
else
{
return $data;
}
}
function show_error($myError)
{
?>
<html>
<body>
<b>Please correct the following error:</b><br />
<?php echo $myError; ?>
</body>
</html>
<?php
exit();
}
?>