如何在 if 语句中调用 php 中的邮件函数


How to call a mail function in php in an if statement?

我正在尝试验证谷歌的nocaptcha验证码,然后将数据发送到电子邮件地址。我有这个php代码:

<?php
function mailsend(){
    $name = trim(strip_tags($_POST['firstname']));
    $email = trim(strip_tags($_POST['email']));
    $message = htmlentities($_POST['bug']);
    $subject = "Bug report submitted :(";
    $to = "someone@example.com";
    $nametitle = "Name:";
    $messagetitle = "Message:";
    $body = <<<HTML
$nametitle
$name
$messagetitle
$message
HTML;
    $headers = "From: $email'r'n";
    $headers = "Content-type: text/html'r'n";
    mail($to, $subject, $body, $headers);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Construct the Google verification API request link.
    $params = array();
    $params['secret'] = 'secret_key'; // Secret key
    if (!empty($_POST) && isset($_POST['g-recaptcha-response'])) {
        $params['response'] = urlencode($_POST['g-recaptcha-response']);
    }
    $params['remoteip'] = $_SERVER['REMOTE_ADDR'];
    $params_string = http_build_query($params);
    $requestURL = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;
    // Get cURL resource
    $curl = curl_init();
    // Set some options
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $requestURL,
    ));
    // Send the request
    $response = curl_exec($curl);
    // Close request to clear up some resources
    curl_close($curl);
    $response = @json_decode($response, true);
    if ($response["success"] == true) {
        echo '<h3 class="alert alert-success">Your Bug report has been sent sucessfully!</h3>';
        mailsend();
    } else {
        echo '<h3 class="alert alert-danger">You have not completed the recaptcha!</h3>';
    }
}
?>

我也在使用此HTML表单:

<form class="col s12" action="" method="post">
            <div class="row">
                <div class="input-field col s6">
                    <input id="first_name" type="text" class="validate" name="firstname" required>
                    <label for="first_name">First Name</label>
                </div>
                <div class="input-field col s6">
                    <input id="last_name" type="text" class="validate" name="lastname">
                    <label for="last_name">Last Name</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s12">
                    <input id="email" type="email" class="validate" name="email" required>
                    <label for="email">Email</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s12">
                    <i class="mdi-editor-mode-edit prefix"></i>
                    <textarea id="icon_prefix2" class="materialize-textarea" name="bug" required></textarea>
                    <label for="icon_prefix2">Please explain your issue clearly here</label>
                </div>
            </div>
            <div class="row">
                <div class="input-field col s12">
                    <div class="g-recaptcha" data-sitekey="site_key"></div>
                </div>
            </div>
            <button class="btn waves-effect waves-light" type="submit" name="action" required>Submit
                <i class="mdi-content-send right"></i>
            </button>
            <span class='msg'><?php echo $msg; ?></span>
        </form>

目前,代码成功验证了验证码,但随后不会将包含表单中的数据的电子邮件发送到所需的电子邮件地址。我的问题是实现谷歌的nocaptcha验证码,然后将表单中的数据发送到电子邮件地址的最佳方法是什么。提前感谢您的帮助!

编辑:已经弄清楚了如何使其工作,并将代码粘贴到此处,供需要帮助的其他任何人使用。
PHP:

    <?php
function mailform(){
    $name = $_POST['firstname'];
    $email = $_POST['email'];
    $message = $_POST['bug'];
    $from = 'Bug Reporting System'; 
    $to = 'someone@example.com'; 
    $subject = 'New Bug Report';
    $message_formatted = "
    <h3>New Bug Reported by $from</h3>
    <h3>From: $name</h3>
    <h5>Bug:</h5>
    <p>$message</p>
    ";
    $headers = "MIME-Version: 1.0" . "'r'n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "'r'n";
    mail($to, $subject, $message_formatted, $headers);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Construct the Google verification API request link.
    $params = array();
    $params['secret'] = 'secret_key'; 
    if (!empty($_POST) && isset($_POST['g-recaptcha-response'])) {
        $params['response'] = urlencode($_POST['g-recaptcha-response']);
    }
    $params['remoteip'] = $_SERVER['REMOTE_ADDR'];
    $params_string = http_build_query($params);
    $requestURL = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;
    // Get cURL resource
    $curl = curl_init();
    // Set options
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => $requestURL,
    ));
    // Send the request
    $response = curl_exec($curl);
    // Close request to clear up some resources
    curl_close($curl);
    $response = @json_decode($response, true);
    if ($response["success"] == true) {
        echo '<h5 class="alert alert-success">Your Bug report has been sent sucessfully!</h5>';
        mailform();
    } else {
        echo '<h5 class="alert alert-danger">You have not completed the recaptcha!</h5>';
    }
}    
?>

原始 HTML 表单不需要更改。我正在使用Materialize框架,如果你想知道为什么有很多标记。

这可能不是问题,但值得一提。根据上面的代码,看起来您正在将电子邮件发送到硬编码 someone@example.com。我没有看到您将收件人设置为与该地址不同的地址的位置。

同样,也许您将其编辑为通用内容,以便您可以发布它,但我只是按照那里写的内容进行操作。