修改联系表格以将用户输入的主题用作电子邮件主题

时间:2019-02-28 14:34:57

标签: javascript php jquery html forms

我有一个使用外部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
        }
    })
});

有什么想法可以做到吗?

P.S。我不包括HTML,因为它应该可以在PHP中读取表单发送的内容,但我需要知道。

1 个答案:

答案 0 :(得分:2)

您只需要更换

$subject         = 'Domain Query';

使用

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

将“ input-name”替换为HTML格式的输入名称