繁体   English   中英

PHP Contactform带有确认电子邮件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM