php - 使用 phpmailer 发送表单时没有页面刷新

标签 php css ajax forms phpmailer

尝试将 phpmailer 与允许您发送表单而无需刷新页面的功能相结合。通常一切看起来都很好,只是电子邮件没有发送。在有关 phpmailer 的清晰代码上,一切正常。

我希望无需刷新页面即可发送电子邮件。也许有人遇到过类似的问题?

index.html

<form name="ContactForm" method="post" action="">
    <div class="form-group">
        <label for="name">Name:</label>
        <input type="text" class="form-control" id="name">
    </div>
    <div class="form-group">
        <label for="email">Email Address:</label>
        <input type="email" class="form-control" id="email">
    </div>
    <div class="form-group">
        <label for="message">Message:</label>
        <textarea name="message" class="form-control" id="message"></textarea>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

<div class="message_box" style="margin:10px 0px;">
</div>
<script>
$(document).ready(function() {
    var delay = 2000;
    $('.btn-default').click(function(e){
        e.preventDefault();
        var name = $('#name').val();
        if(name == ''){
            $('.message_box').html(
                '<span style="color:red;">Enter Your Name!</span>'
            );
            $('#name').focus();
            return false;
        }

        var email = $('#email').val();
        if(email == ''){
            $('.message_box').html(
                '<span style="color:red;">Enter Email Address!</span>'
            );
            $('#email').focus();
            return false;
        }
        if( $("#email").val()!='' ){
            if( !isValidEmailAddress( $("#email").val() ) ){
                $('.message_box').html(
                    '<span style="color:red;">Provided email address is incorrect!</span>'
                );
                $('#email').focus();
                return false;
            }
        }

        var message = $('#message').val();
        if(message == ''){
            $('.message_box').html(
                '<span style="color:red;">Enter Your Message Here!</span>'
            );
            $('#message').focus();
            return false;
        }   

        $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "name="+name+"&email="+email+"&message="+message,
            beforeSend: function() {
                $('.message_box').html(
                    '<img src="Loader.gif" width="25" height="25"/>'
                );
            },       
            success: function(data)
            {
                setTimeout(function() {
                    $('.message_box').html(data);
                }, delay);

            }
        });
    }); 
});
</script>

ajax.php

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// if(isset($_POST['submit'])){
    // $name= $_POST['name'];
    // $email= $_POST['email'];
    // $tel= $_POST['tel'];
    // $message= $_POST['message'];
if ( ($_POST['name']!="") && ($_POST['email']!="")){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 0;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.mailtrap.io';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'bf4908e76c4186';                 // SMTP username
    $mail->Password = 'fe1e3963078670';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom($email, $name);
    $mail->addAddress('pawel@gmail.com', 'Joe User');     // Add a recipient
    $mail->addAddress('pawel@gmail.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');

    //Attachments
    // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content 
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = $message;


    $mail->send();
if($send){
    echo "<span style='color:green; font-weight:bold;'>
    Thank you for contacting us, we will get back to you shortly.
    </span>";
}
else{
    echo "<span style='color:red; font-weight:bold;'>
    Sorry! Your form submission is failed.
    </span>";
    }
}

向我显示副标题:“感谢您联系我们,我们会尽快回复您。”但不幸的是邮箱不是空的。

最佳答案

除了 try/catch 问题,您还有其他问题。

SMTPSecure = 'tls'Port = 465 的组合将不起作用;要么更改为 ssl 模式,要么更改 Port = 587。这在故障排除指南中有详细记录。

不要使用提交者的地址作为发件人地址;这是伪造的,会导致您的邮件因 SPF 失败而被退回或被垃圾邮件过滤。将您自己的地址放在表单地址中,并将提交者的地址放在回复中 - 请参阅 PHPMailer 提供的联系表单示例。

关于php - 使用 phpmailer 发送表单时没有页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316962/

相关文章:

ubuntu - phpinfo() 什么也没显示,但我所有其他 php 脚本都可以正常工作

php - 仅将键插入到 json 中,不插入值

php - 如何仅在特定页面中 bundle.min.css(在我的例子中是 contact.php)

html - 如果谷歌字体不可用,如何指定网络安全用户默认字体

css - 造型 Fancybox 2 AJAX

php - 准备语句给出错误

javascript - FontAwesome 元素太大?

css - div 行没有正确排列

Java Ajax 显示进度

javascript - 使用 API V3 的主要谷歌地图故障