2016-11-04 49 views
0

我用PHPmailer做了一個聯繫表格,但我想要做的是,填寫contactform的人在他的電子郵件中獲得他填寫的內容的副本。這個「email」是他填寫的電子郵件,在「email」字段中填寫。 (如果那有意義的話)。PHP Mailer用戶誰填寫獲取副本

你們能幫我嗎?這是我的代碼。

HTML/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']); 
?> 

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 = 'Dit is een test voor birsken.nl Negeer dit'; 
    $mail->Body = 'From: ' . $fields['name'] . ' ('. $fields['email'] .')<p>'. $fields['message'] .'</p>'; 

    $mail->FromName = $fields['name']; 
    $mail->AddAddress('', ''); 

    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'); 
+1

'$ MAIL-> AddAddress($字段['email'],'');'? –

+0

驗證電子郵件...我真的不知道如何更好地解釋它。 –

+0

@u_mulder –

回答

0

可以CC和根據PHPMailer的文檔BCC:

$mailer->AddBCC("[email protected]", "test"); 
$mailer->AddCC("[email protected]", "bla"); 
+0

這不起作用。我只是給了我這個錯誤。 http://prntscr.com/d2z4hm –

+0

@RainierLaan你使用了'$ mail-> AddBCC(「[email protected]」,「test」);'(你的變量是'$ mail'而不是'$ mailer '在上面的例子中)? – martincarlin87

+0

我試過了,'$ mail-> AddCC($ fields ['email'],$ fields ['name']);'但是它不起作用。我填寫的電子郵件不會獲得副本 –