2014-10-11 43 views
1

我做了一个php表单,但我无法想象如何让它自动回复填充的电子邮件。 我想发送一个确认信息给刚使用过我们的表单的人它,但我不知道如何使它。PHP自动回复器

这里我的代码示例。 我想发送一封自定义邮件到$ email,我该怎么做?!

我做了一些改变,但我不recive电子邮件喷射。 Maby我确实了解你,但是你能否再次检查我。

<?php 
/* Set e-mail recipient */ 
$myemail = "[email protected]"; 

/* 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: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

mail($email, $subject, $message, $headers); 
?> 

<?php 
exit(); 
} 
?> 
+0

检查PHP的mail()函数。 – 2014-10-11 10:40:02

+0

在您的操作后添加php邮件()... – 2014-10-11 10:45:05

+0

当您尝试此代码时会发生什么情况? – webeno 2014-10-11 10:45:45

回答

0

您设置此脚本的方式存在多个错误,请让我尝试将它们全部指向您。

首先,你用header('Location...,不论是否有任何的show_error功能实际上被呼应/它上面印着的,这将导致一个错误...

警告:不能修改标题信息 - 已经

这是因为你不能输出任何东西头(阅读更多有关此内容的PHP documentation of header)前发送标题。

因此,为了避免这种情况,您必须以某种方式告诉您的脚本存在错误,并检查它,然后只有在没有错误时才允许执行header

就你而言,我会从你的check_input函数中删除$problem变量,并将它变成一个错误消息数组。这样,您就能够输出多个错误信息,如果有没有被正确填写多个表单输入:

$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); 
} 

我知道还有很多其他的方法可以做到这一点,但我发现这是通过提供最理想的结果之一来修改现有脚本的人。

注意:我知道花括号({})并不总是必要的,但它可能是最好的开始。

+0

我有这个代码从互联网我只做了几个变化我的PHP技能真的很烂 – 2014-10-11 11:55:29

+0

审查我的答案,尝试做出相应的编辑和看看它是否有效......另外,将您的编辑内容发布在原始问题中,而不是作为答案;尝试也使他们更短,保持相关性和具体“不工作”不是一个错误信息;) – webeno 2014-10-11 11:59:12

+0

OKe感谢提示其在我的网站上发表的所有帖子,所以我不知道所有这些工作。我已经编辑了代码。需要创建邮件并发送给用户的电子邮件。我已经尝试过,但我不会自己收到邮件,只有[email protected]帐户才能收到电子邮件。查看我的代码以了解更改。感谢您的帮助 – 2014-10-11 12:13:56

0

这是一个工作脚本! 其实很简单。 感谢您的帮助!

<?php 
/* Set e-mail recipient */ 
$myemail = '[email protected]' . ', '; // 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(); 
} 
?>