【问题标题】:Modify contact form to use user entered subject as email subject修改联系表格以使用用户输入的主题作为电子邮件主题
【发布时间】:2019-02-28 14:34:57
【问题描述】:

我有一个使用外部 SMTP 服务器和 Recaptcha 的 PHP 联系表单,效果很好。

我正在尝试修改它,以便用户输入的主题是电子邮件的主题(而不是当前的静态主题),并且用户输入的电子邮件是默认的回复电子邮件。

这是我的 PHP 代码:

<?php
require './PHPMailer-master/PHPMailerAutoload.php';
require './recaptcha-master/src/autoload.php';
$fromEmail       = 'no-reply@domain.com';
$fromName        = 'Domain Contact';
$sendToEmail     = 'support@domain.com';
$sendToName      = 'Domain Support';
$subject         = 'Domain Query';
$recaptchaSecret = 'secret_here';
$fields          = array(
    'firstname' => 'Firstname',
    'lastname' => 'Lastname',
    'subject' => 'Subject',
    'email' => 'Email',
    'message' => 'Message'
);
$okMessage       = 'Thank you for getting in touch! We will get back to you with your query shortly';
$errorMessage    = 'There was an error while submitting the form. Please try again later';
error_reporting(E_ALL & ~E_NOTICE);
try {
    if (!empty($_POST)) {
        if (!isset($_POST['g-recaptcha-response'])) {
            throw new \Exception('ReCaptcha is not set.');
        }
        $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
        $response  = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
        if (!$response->isSuccess()) {
            throw new \Exception('ReCaptcha was not validated.');
        }
        $emailTextHtml .= "<table>";
        foreach ($_POST as $key => $value) {
            if (isset($fields[$key])) {
                $emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
            }
        }
        $emailTextHtml .= "</table>";
        $mail = new PHPMailer;
        $mail->setFrom($fromEmail, $fromName);
        $mail->addAddress($sendToEmail, $sendToName);
        $mail->addReplyTo($from);
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $emailTextHtml;
        $mail->msgHTML($emailTextHtml);
        $mail->isSMTP();
        $mail->SMTPDebug   = 0;
        $mail->Debugoutput = 'html';
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->Host        = 'email-smtp.eu-west-1.amazonaws.com';
        $mail->Port        = 587;
        $mail->SMTPSecure  = 'tls';
        $mail->SMTPAuth    = true;
        $mail->Username    = 'username';
        $mail->Password    = 'password';
        if (!$mail->send()) {
            throw new \Exception('There was a problem sending your message. Please try again later' . $mail->ErrorInfo);
        }
        $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'];
}

这是我的 JavaScript:

$(function() {
    window.verifyRecaptchaCallback = function(response) {
        $('input[data-recaptcha]').val(response).trigger('change')
    }
    window.expiredRecaptchaCallback = function() {
        $('input[data-recaptcha]').val("").trigger('change')
    }
    $('#domain-contact').validator();
    $('#domain-contact').on('submit', function(e) {
        if (!e.isDefaultPrevented()) {
            var url = "contact.php";
            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function(data) {
                    var messageAlert = 'alert-' + data.type;
                    var messageText = data.message;
                    var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                    if (messageAlert && messageText) {
                        $('#domain-contact').find('.messages-contact').html(alertBox);
                        $('#domain-contact')[0].reset();
                        grecaptcha.reset()
                    }
                }
            });
            return !1
        }
    })
});

有什么想法可以做到吗?

附:我不包括 HTML,因为它应该可以在 PHP 中读取表单发送的内容,但如果需要,请让我知道。

【问题讨论】:

  • 您的问题到底是什么?这其中的哪一部分给你带来了麻烦?您似乎知道如何在电子邮件上设置主题。看来您知道如何接收发布的数据。我没看到你迷路了。
  • 我不懂 PHP。这是我在网上找到的 3 种形式的集合,每种形式分别用于 recaptcha、external-smtp 和 ajax。下面的答案回答了它所以nvm。

标签: javascript php jquery html forms


【解决方案1】:

你只需要替换

$subject         = 'Domain Query';

$subject         = $_POST['input-name'];

将“input-name”替换为 HTML 表单中输入的名称

【讨论】:

  • 谢谢。效果很好。
猜你喜欢
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多