Rainier Laan Rainier Laan - 17 days ago 5
HTML Question

PHP Contactform with confirmation email

I made a contactform which send (if filled in) a mail to the email that's in the code. I want a contactform that send a confirmation email to the person that fills in the form AND to the owner of the site. So the customer knows he filled in the form in the site.
If this makes any sense? I dont know how to explain it better.

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('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');


I hope you guys can help me out with this and can provide me with code i can use. If something i said are unclear please say so. This code is without this function that i explained above, This is only with the function that the owner gets a mail not the customer. Rainier laan.

Answer

Use below code for 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'); //added mail id of owner
    $mail->AddAddress($fields['email] , $fields['name]); //added mail id of user

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