Problem with sending the response back to JS from PHP

I want to send to a mail with a click on the HTML. I used ajax to call a PHP file. I included the PHP mailer code to send an email. And I also include a print json_encode(“Success”) at the bottom. I couldn’t send the success message from php back to JS. I have used Print and many other output commands in PHP its not working.

$.ajax({
                    url:"api/collect_money.php",


    enter code here

    type:"POST",
                    data:ajax_data,
                    async:false,
                    success:function(response)
                    {

                        console.log(response,"hello");
                    }
                });






Below is my PHP Codes:

function sendmail($bills_count_data)
    {
        //print_r($bills_count_data[0]['end_date']);
        //Load Composer's autoloader
        require 'php/vendor/phpmailer/phpmailer/src/Exception.php';
        require 'php/vendor/phpmailer/phpmailer/src/PHPMailer.php';
        require 'php/vendor/phpmailer/phpmailer/src/SMTP.php';
        $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
        try 
        {
            //Server settings
            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = 'mail@example.com';                 // SMTP username
            $mail->Password = 'password';                           // SMTP password
            $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom('from@example.com', 'Mailer');
            // Add a recipient
            $mail->addAddress('mail@example.com');    

            //$mail->addAddress('another@example.com');           // Name is optional
            // $mail->addReplyTo('info@example.com', 'Information');
            //$mail->addCC('cc@example.com');
            //$mail->addBCC('bcc@example.com');
            //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 = 'Collect Money Notification';
            $mail->Body    = "Collection has been made on ".$bills_count_data[0]['end_date']." The list of bills: one's ".$bills_count_data[0]['one']." two's ".$bills_count_data[0]['two']."";

            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
            $mail->send();
            //echo 'Message has been sent';
        } 
        catch (Exception $e) 
        {
            echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        }
    }

    sendmail($bills_count_data);
}




print ("success");

I don’t see any response on the console. when I execute this code.

Where is the section of your PHP code that extracts the contents of the $_POST array and converts it into the variable you pass into your sendmail() function?

Other than that, it’s hard to see anything specific wrong with the code, presuming you’ve missed out the opening and closing PHP tags for clarity on the forum. Is your PHP code definitely being executed, that is, the PHP code is stored in the correct place? If you add this just after your opening PHP tag:

echo "In PHP code";
exit();

and call it, do you get that message in the console? If not, then it suggests that your PHP is not being called.

You seem to have mismatched open and close braces in your PHP code, is that because you’ve edited stuff out at the top? I’m not sure whether more closes than opens will throw a parse error or whether it will just ignore the extra close brace.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.