2016-11-22 82 views
-1

我做了这派(如果填写)邮件到这是在代码的电子邮件联系形式。我想要一个联系表单,向填写表格的人员和网站所有者发送确认电子邮件。所以客户知道他在网站上填写了表格。 如果这有什么意义?我不知道如何更好地解释它。PHP联系形式与确认邮件

CODE:的index.php

<?php 

session_start(); 

require_once 'helpers/security.php'; 

$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : []; 
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : []; 
?> 



<!DOCTYPE html> 
    <html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Contact form</title> 

<link rel="stylesheet" href="css/style.css"/> 
<script src="js/script.js"></script> 
</head> 
<body> 
<div class="contact"> 

    <?php if(!empty($errors)): ?> 
     <div class="panel"> 
      <ul> 
       <li> 
        <?php echo implode('</li><li>', $errors); ?> 
       </li> 
      </ul> 
     </div> 
    <?php endif; ?> 
    <form action="contact.php" method="post"> 
     <label> 
      Your name* 
      <input type="text" name="name" id="name" autocomplete="off" <?php echo isset($fields['name']) ? 'Value="' . e($fields['name']) . '"' : '' ?>> 
     </label> 
     <label> 
      Your email address * 
      <input type="email" name="email" id="email" autocomplete="off" <?php echo isset($fields['email']) ? 'Value="' . e($fields['email']) . '"' : '' ?>> 
     </label> 
     <label> 
      Your message * 
      <textarea name="message" id="contact" rows="8"><?php echo isset($fields['message']) ? e($fields['message']) : '' ?></textarea> 
     </label> 

     <input type="submit" value="Send"> 

     <p class="muted">* Means a required field</p> 
    </form> 
</div> 



</body> 
</html> 

<?php 
unset($_SESSION['errors']); 
unset($_SESSION['fields']); 
?> 

CODE:contact.php

<?php 

session_start(); 

require_once "libs/phpmailer/PHPMailerAutoload.php"; 

$errors = []; 


if(isset($_POST['name'], $_POST['email'], $_POST['message'])) { 

$fields = [ 
    'name' => $_POST['name'], 
    'email' => $_POST['email'], 
    "message" => $_POST['message'] 
]; 

foreach($fields as $field => $data) { 
    if(empty($data)){ 
     $errors[] = 'The ' . $field . ' field is required.'; 
    } 
} 

    // 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl 
    // 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl 
    // 110 is voor inkomende email deze is POP3 en 
if(empty($errors)){ 
    $mail = new PHPMailer; 

    $mail->isSMTP(); 
    $mail->SMTPAuth = true; 

    $mail->Host = ''; 
    $mail->Username = ''; 
    $mail->Password = ''; 
    $mail->SMTPSecure = 'tls'; 
    $mail->Port = 587; 

    $mail->isHTML(); 
    $mail->SMTPDebug = 2; 

    $mail->Subject = 'Subject'; 
    $mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .') <p>'. $fields['message'] .'</p>'; 

    $mail->FromName = $fields['name']; 

    $mail->AddAddress('[email protected]', 'Rainier Laan'); 

    if($mail->send()){ 
     header('Location: bedankt.php'); 
     die(); 
    } else { 
     echo $mail->ErrorInfo; exit; 
    } 
} 

} else { 
$errors[] = 'Something went wrong.'; 
} 

$_SESSION['errors'] = $errors; 
$_SESSION['fields'] = $fields; 

header('location: index.php'); 

我希望你们能帮助我走出这一点,并能为我提供的代码我可以用。如果我说的内容不清楚,请说出来。这个代码没有这个我上面解释过的函数,这只是与所有者获取邮件而不是客户的函数。雷尼尔拉恩。

回答

2

使用下面的代码contact.php

<?php 

    session_start(); 

    require_once "libs/phpmailer/PHPMailerAutoload.php"; 

    $errors = []; 


    if(isset($_POST['name'], $_POST['email'], $_POST['message'])) { 

    $fields = [ 
     'name' => $_POST['name'], 
     'email' => $_POST['email'], 
     "message" => $_POST['message'] 
    ]; 

    foreach($fields as $field => $data) { 
     if(empty($data)){ 
      $errors[] = 'The ' . $field . ' field is required.'; 
     } 
    } 

     // 587 is voor uitgaande email deze is SSL en SMTP.ziggo.nl 
     // 993 is voor inkomende email deze is TLS en IMAP.ziggo.nl 
     // 110 is voor inkomende email deze is POP3 en 
    if(empty($errors)){ 
     $mail = new PHPMailer; 

     $mail->isSMTP(); 
     $mail->SMTPAuth = true; 

     $mail->Host = ''; 
     $mail->Username = ''; 
     $mail->Password = ''; 
     $mail->SMTPSecure = 'tls'; 
     $mail->Port = 587; 

     $mail->isHTML(); 
     $mail->SMTPDebug = 2; 

     $mail->Subject = 'Someone filled in your contactform'; 
     $mail->Body = $fields['name'].' filled in your form with the following message: ' .$fields['message']; 

     $mail->FromName = $fields['name']; 

     $mail->AddAddress('[email protected]', 'Rainier Laan'); //added mail id of owner 

     if($mail->send()){ 

      $mail = new PHPMailer; 

      $mail->isSMTP(); 
      $mail->SMTPAuth = true; 

      $mail->Host = ''; 
      $mail->Username = ''; 
      $mail->Password = ''; 
      $mail->SMTPSecure = 'tls'; 
      $mail->Port = 587; 

      $mail->isHTML(); 
      $mail->SMTPDebug = 2; 

      $mail->Subject = 'Confirmation contactform'; 
      $mail->Body = 'Thank you for filling in our form.<br> Message: <p>'. $fields['message'] .'</p>'; 

      $mail->FromName = 'Owner'; 

      $mail->AddAddress($fields['email] , $fields['name]); //added mail id of user 
      if($mail->send()){ 
       header('Location: bedankt.php'); 
       die(); 
      } 
      else{ 
       exit; 
      } 
     } else { 
      echo $mail->ErrorInfo; exit; 
     } 
    } 

    } else { 
    $errors[] = 'Something went wrong.'; 
    } 

    $_SESSION['errors'] = $errors; 
    $_SESSION['fields'] = $fields; 

    header('location: index.php'); 
+0

我觉得这可能是工作。唯一的一点是,我不能尝试这个,因为今天,我从我的供应商填写的电子邮件数据我也保护或东西,因为它给了我一个很大的错误。也许你们知道该怎么做?我知道你可以解决这个错误,因为我之前做过。以下是错误:http://prntscr.com/daf4jc –

+0

@RainierLaan随时提出新的问题。 –

+0

我不工作。您提供的代码只会向用户填写的邮件发送一封电子邮件,而不会同时发送给我的个人邮件和用户邮件。它所做的是,它发送一封电子邮件给我的个人电子邮件中的用户电子邮件,该电子邮件位于代码中。 (如果你仔细阅读,你会明白)还有我的个人电子邮件。你能帮我吗? –