[英]PHP Contactform with confirmation email
我制作了一個聯系表單,用於將代碼(如果已填寫)發送到代碼中的電子郵件。 我需要一個聯系表單,該表單將確認電子郵件發送給填寫表格的人,並發送給網站的所有者。 因此客戶知道他在站點中填寫了表格。 這有意義嗎? 我不知道如何更好地解釋它。
代碼: 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']);
?>
代碼: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('rainier.laan@home.nl', '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');
我希望你們能幫我解決這個問題,並能提供我可以使用的代碼。 如果我說的不清楚,請這樣說。 該代碼沒有上面我解釋過的此功能,這僅與所有者獲得郵件而不是客戶獲得郵件的功能有關。 雷尼爾·蘭恩。
將以下代碼用於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('rainier.laan@home.nl', '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');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.