使用 Bootstrap 将回复电子邮件添加到联系表单

Adding a reply email to a contact form using Bootstrap

提问人:Zeeba 提问时间:11/15/2023 最后编辑:Peter MortensenZeeba 更新时间:11/17/2023 访问量:78

问:

我不擅长 PHP。我在这里看到另一篇帖子问同样的事情,但代码不同,我尝试了一些东西来看看它是否有效,但我不确定我需要更改什么才能使其正常运行。

我希望客户的电子邮件地址在您点击回复时出现在发送到中。现在,当您点击回复时,它会填写联系表单电子邮件地址。

{
    <?php
    // Require ReCaptcha class
    require('recaptcha-master/src/autoload.php');

    // Configure
    // an email address that will be in the From field of the email.
    $from = 'Contact form <[email protected]>';

    // An email address that will receive the email with the output of the form
    $sendTo = 'Contact form <[email protected]>';

    // Subject of the email
    $subject = 'New message from Customer';

    // Array variable name => Text to appear in the email
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message', 'tel' => 'Telephone Number' , 'add' => 'Address' , 'comp' => 'Company Name', 'product' => 'Product');

    // Message that will be displayed when everything is OK :)
    $okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon! <br> You\'ll be redirected in about 5 secs. If not, click <a href="http://www.rs-cryo-equipment.com/contact-us.php">Here.</a>.';

    header("refresh:5;url=http://www.back to/contact-us.php");

    // If something goes wrong, we will display this message.
    $errorMessage = 'There was an error while submitting the form. Please try again later';

    // ReCaptch Secret
    $recaptchaSecret = 'SSHHHHH';

    // Let's do the sending

    // If you are not debugging and don't need error reporting,
    // turn this off by error_reporting(0);
    error_reporting(E_ALL & ~E_NOTICE);

    try {
        if (!empty($_POST)) {

            // Validate the ReCaptcha, if something is wrong, we throw an Exception,
            // i.e. code stops executing and goes to catch() block

            if (!isset($_POST['g-recaptcha-response'])) {
                throw new \Exception('ReCaptcha is not set.');
            }

            // Do not forget to enter your secret key
            // from https://www.google.com/recaptcha/admin

            $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());

            // We validate the ReCaptcha field together with the user's IP address

            $response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);

            if (!$response->isSuccess()) {
                throw new \Exception('ReCaptcha was not validated.');
            }

            // Everything went well, and we can compose the message, as usual

            $emailText = "You have a new message from your contact form\n=============================\n";

            foreach ($_POST as $key => $value) {
                // If the field exists in the $fields array, include it in the email
                if (isset($fields[$key])) {
                    $emailText .= "$fields[$key]: $value\n";
                }
            }

            // All the neccessary headers for the email.
            $headers = array('Content-Type: text/plain; charset="UTF-8";',
                'From: ' . $from,
                'Reply-To: ' . $from,
                'Return-Path: ' . $from,
            );

            // Send email
            mail($sendTo, $subject, $emailText, implode("\n", $headers));

            $responseArray = array('type' => 'success', 'message' => $okMessage);
        }
    } catch (\Exception $e) {
        $responseArray = array('type' => 'danger', 'message' => $e->getMessage());
    }

    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        $encoded = json_encode($responseArray);

        header('Content-Type: application/json');

        echo $encoded;
    } else {
        echo $responseArray['message'];
    }

}
php 电子邮件 bootstrap-5 联系表单

评论

1赞 Davi Amaral 11/15/2023
什么不起作用?你需要说出你想做什么,以及错误是什么!
0赞 Zeeba 11/15/2023
天哪,我是个白痴。我希望客户电子邮件在您点击回复时发送到,现在当您点击回复时,它会用联系表单电子邮件地址填写它。
0赞 Peter Mortensen 11/17/2023
为什么代码以“”开头(在“”之前)?{<?php

答:

1赞 VonC 11/17/2023 #1

要修改您的 PHP 代码,使客户的电子邮件成为联系表单中的地址,您需要首先从 POST 数据中提取电子邮件。假设电子邮件的表单字段名为“email”:Reply-To

$customerEmail = $_POST['email'] ?? null;

然后,修改标题以在客户的电子邮件中包含“回复”,如 [PHP mail() function][https://www.php.net/manual/en/function.mb-send-mail.php] 所示。

if ($customerEmail) {
    $headers[] = 'Reply-To: ' . $customerEmail;
}

注意:您可能需要在使用电子邮件之前对其进行验证或清理
例如,请参阅“如何在邮件之前清理PHP中的用户输入?”或“PHP电子邮件验证 - 过滤器验证和过滤器清理”
"

您的脚本将是:

<?php
// previous code remains unchanged

$customerEmail = filter_var($_POST['email'] ?? null, FILTER_SANITIZE_EMAIL);

// Validate the email
if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
    $customerEmail = null; // Invalid email, set to null
}

// previous code remains unchanged

// All the necessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Return-Path: ' . $from,
);

// Add Reply-To header
if ($customerEmail) {
    $headers[] = 'Reply-To: ' . $customerEmail;
}

// (rest of your code)
?>

评论

1赞 VonC 11/17/2023
@Zeeba没问题。如果您需要更多详细信息,请告诉我。