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.