带有Recaptcha 2.0的PHP邮件

时间:2015-06-30 17:41:53

标签: php html email recaptcha

我试图让这个电子邮件表单使用php工作,但遗憾的是我不能。会发生什么事情,当我按下提交时,它只是处理和chrome说“等待localhost”。

一些背景:我在本地使用WAMP并使用端口465编辑了带有gmail的smtp服务器的php.ini。我正在使用默认的邮件功能但是我很怀疑它并且认为因为谷歌需要身份验证等等我需要使用像sendmail这样的库。话虽如此,我只是想在核心发送一封电子邮件而不关心使用哪个库。

HTML表单:

<div id="email">
                            <div class="email-icon center">
                                <i class="fa fa-envelope-o fa-5x"></i>
                            </div>
                            <form id="contact" method="POST" action="sendmail.php">
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="fa fa-pencil"></i>
                                    </span>
                                    <input type="text" name="name" id="name" class="form-control" placeholder="Name" required>
                                </div>
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="fa fa-pencil"></i>
                                    </span>
                                    <input type="text" name="email" class="form-control" placeholder="Email" required>
                                </div>
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="fa fa-pencil"></i>
                                    </span>
                                    <input type="text" name="subject" class="form-control" placeholder="Subject" required>
                                </div>
                                <div class="input-group">
                                    <span class="input-group-addon">
                                        <i class="fa fa-pencil"></i>
                                    </span>
                                    <textarea name="message" class="form-control status-box" id="message" rows="4" placeholder="What's on your mind?" required></textarea>
                                </div>
                                <div class="g-recaptcha" data-sitekey="6LdQ6wgTAAAAAHOj3FnhjZF6FMmUCC_r4FMdx2m2" data-theme="dark"></div>
                                <div class="input-group">
                                    <div class="container">
                                        <div class="row">
                                            <div class="col-md-4 col-md-offset-4 center">
                                                <span class="input-group-button">
                                                    <button class="btn btn-default" name="submit" type="submit" value="Submit"> SEND </button> 
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            <div class="response center"></div>
                        </div>

PHP代码:

<?php
    require ("src/autoload.php");

    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = trim($_POST["subject"]);
    $message = $_POST["message"];
    $to = "example@gmail.com";
    $headers = "From: ".$email."\r\n";

    $secret = "MY SECRET CODE";
    $recaptcha = new \ReCaptcha\ReCaptcha($secret);
    $resp = $recaptcha->verify($_POST["g-recaptcha-response"], $_SERVER["REMOTE_ADDR"]);
    if (!$resp->isSuccess()) {
        $captchaErr = "The reCAPTCHA wasn't entered correctly. Please try it again.";
    } 
    else {
        $validCaptcha = true;
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $emailErr = "Invalid email format";
    }
    else{
        $validEmail = true;
    }

    if ( $validCaptcha && $validEmail) {
        $sendMail = mail($to, $subject, $message, $headers);
    }

    if ($sendMail) {
        $sendMailSucc = "";
    } 
    else {
        $sendMailErr = "";
    }
?>

PHP.INI邮件功能

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 465

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = example@gmail.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path = 

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on NT, not valid in Windows 95).
;mail.log = syslog

另外一个注意事项,我不知道为什么,但WAMP的PHP-smtp扩展有一个危险信号,我认为这也可能导致问题。

我将不胜感激任何帮助。如果有人能够让我知道如何在不打开新页面并就地发送电子邮件的情况下实现这一目标,我将不胜感激。

0 个答案:

没有答案