Data not transfering from Ajax to php to mail - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, May 11, 2023

Data not transfering from Ajax to php to mail

I followed this tutorial : https://www.youtube.com/watch?v=DKq1n-awLcw On how to implement a working contact form in my website.

Everything went fine, until I sent the first test mails and discovered that the mails are indeed sent, but without any informations : Empty mail received.

I'm not an expert in jquery or php, but I get how it works, and I can't seem to find why it is not working as intended.

I tried to tweek some variables here and there, but without any results.

Here is the HTML :

<form id="contactform" class="form-element" action="php/contact.php" method="post">
          
                    <label class="form-field">
                        <span>Name</span>
                        <input id="name" class="form-component" type="text" name="name" autocomplete="off" required>
                    </label>
            
                    <label class="form-field">
                        <span>Email</span>
                        <input id="email" class="form-component" type="email" name="email" autocomplete="off" required>
                    </label>
            
                    <label class="form-field">
                        <span>Subject</span>
                        <input id="subject" class="form-component" type="text" name="subjects" autocomplete="off" required>
                    </label>
            
                    <label class="form-field">
                        <span>Message</span>
                        <textarea id="message" class="form-text-area" name="message" autocomplete="off" required></textarea>
                    </label>
            
                    <div class="form-field">
                        <button class="form-submit" type="button" onclick="sendEmail()" value="Send an email.">Submit</button>
                    </div>
                </form>

Here the script :

<script type="text/javascript">
        function sendEmail(){
            var name = $("#name");
            var email = $("#email");
            var subject = $("#subject");
            var message = $("#message");

            if(isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(message)){
                $.ajax({
                    url: 'php/contact.php',
                    method: 'POST',
                    dataType: 'json',
                    data:{
                        name: name.val(),
                        email: email.val(),
                        subject: subject.val(),
                        message: message.val() 
                    }, success: function(response){
                        $('#contactform')[0].reset();
                        $('.confirm-title').text("Message sent successfully!");
                    }
                });
            }
        }

        function isNotEmpty(caller){
            if(caller.val()=="") {
                caller.css('border', '1px solid red');
                return false;
            }
            else {
                caller.css('border', '');
                return true;
            }
        }
    </script>

And finaly here is the PHP document :

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

if(isset($_POST['name']) &&  isset($_POST['email'])){
    $name               =   $POST['name'];
    $email              =   $POST['email'];
    $subject            =   $POST['subject'];
    $message            =   $POST['message'];

    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/Exception.php";
    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/PHPMailer.php";
    require_once $_SERVER['DOCUMENT_ROOT']."/php/includes/phpmailer/SMTP.php";

    $mail = new PHPMailer();

    // SMTP Settings
    $mail->isSMTP();
    $mail->Host         =   "secret";
    $mail->SMTPAuth     =   true;
    $mail->Username     =   'secret';
    $mail->Password     =   'secret';
    $mail->Port         =   secret;
    $mail->SMTPSecure   =   "ssl";

    // Email settings
    $mail->isHTML(true);
    $mail->setFrom($email, $name);
    $mail->addAddress("secret");
    $mail->Subject      =   ("$email ($subject)");

    $MailBody           =   "From: ".$name."<br>";
    $MailBody           .=   "Email: ".$email."<br>";
    $MailBody           .=   "Message: ".$message."<br>";

    $mail->Body         =   $MailBody;

    if($mail->send()){
        $status = "success";
        $response = "Email is sent !";
    }
    else {
        $status = "failed";
        $response = "Something is wrong: <br>".$mail->ErrorInfo;
    }

    exit(json_encode(array("status" => $status, "response" => $response)));
}

I do receive the "Message sent successfully!" message on the website.

I already checked the "Transfer data from Ajax to php" post, and everything seems to be fine so..



source https://stackoverflow.com/questions/76221426/data-not-transfering-from-ajax-to-php-to-mail

No comments:

Post a Comment