首页 > 解决方案 > 我无法让我的 php 邮件程序使用发布数据发送邮件

问题描述

我在使用 phpmailer 和 ajax 时遇到了一些问题。我对 ajax 很陌生,还没有完全理解它,所以这可能有一个非常明显的修复,但我们开始了。

因此,当我只是将我的 php 邮件程序脚本放在一个页面中并使用设置值访问它时,我可以发送邮件。但是每当我尝试将表单数据发送到脚本时,某些东西就不起作用。

这是我的jQuery

$(document).ready(function(){
    $(".contact-form").on("submit",function(e){
      e.preventDefault();

        function validateEmail($email) {
            var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
            return emailReg.test( $email );
        }
    

        var valid = true;   
        
        if(!$("#name").val()) {
            $("#userEmail").css('border','2px solid #FFFFDF');
            valid = false;
        }
        if( !validateEmail($(' #email').val())) { 
            valid = false
        }
        if(!$("#subject").val()) {
            $("#subject").css('border','2px solid #FFFFDF');
            valid = false;
        }
        if(!$("#message").val()) {
            $("#message").css('border','2px solid #FFFFDF');
            valid = false;
        }
        
    
    //send data to php file
    
        
        var isValid;    
        isValid = valid

        if(isValid) {
            var name = $("#name").val();
            var email = $("#email").val();
            var subject = $("#subject").val();
            var message = $("#message").val();
            jQuery.ajax({
                url: "includes/sendmail.inc.php",
                type : "POST",
                data:{name:name,email:email,subject:subject,message:message},
                success:function(data){
                $(".err_msg").text("Send!");
                $(".err_msg").css('background-color', 'green')
                $(".err_msg").show()
                },
                error:function (){}
            });
        } else {
             $(".err_msg").text("Oops! check your input.");
             $(".err_msg").css('background-color', 'red')
             $(".err_msg").show()
            }
    });
});

这是我的php

<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];


// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load Composer's autoloader
require 'vendor/autoload.php';

$name = $_POST['name'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
   

    //Server settings
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'mail.example@example.com';                    // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    $mail->Username   = 'example@example.com';                     // SMTP username
    $mail->Password   = 'example123';                               // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
    $mail->Port       = 587 ;                                    // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above

    //Recipients
    $mail->setFrom( $email , $name);
    $mail->addAddress('example@example.com', 'John Doe');     // Add a recipient
    $mail->addReplyTo($email , $name);
    


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


    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

所以这就是我到目前为止所拥有的,我希望你们能弄清楚我做错了什么,因为我已经坚持了大约两天了。

问候弗洛里斯

标签: javascriptphpjqueryajaxemail

解决方案


是的,我想通了,谢谢!所以出了问题的是,由于某种原因它不会发送,因为 FROM 电子邮件地址是一个 gmail 帐户,而我使用的是我自己的域 smtp,这导致了它的出现


推荐阅读