I have simple code in PHP and PHP library PHPMailer where I have email.php where email is added to database only if is not already in database. When is or not there is messages for adding email to database and not adding. All this thing working with AJAX. All working good without require 'newuser.php';.
I added newuser.php file to else on the bottom which is using PHPMailer functions for sending emails. When Its in else on the bottom added require 'newuser.php'; in AJAX will be done fail branch not done branch which I want.
So all going well without require 'newuser.php'; and when its added to else on the bottom will be done fail branch not done branch so something going wrong there and i dont know what.
My email.php:
<?php
if (empty($_POST['email'])) return http_response_code(404);
$emaildb = mysqli_connect("localhost", "root", "root", "dbdbdbd");
if(!$emaildb) {
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$email = mysqli_real_escape_string($emaildb, $_REQUEST['email']);
$sql = "INSERT INTO emails (email)
SELECT '$email'
FROM DUAL
WHERE NOT EXISTS
(SELECT 1
FROM emails
WHERE email = '$email')
LIMIT 1";
$result = mysqli_query($emaildb, $sql);
header('Content-type: application/json');
if(mysqli_insert_id($emaildb) == 0) {
$dataDuplicate = array(
"existing" => true,
"message" => "Váš e-mail už máme, nižšie si môžete stiahnúť potrebné dokumenty."
);
echo json_encode($dataDuplicate);
} else {
require 'newuser.php';
$dataSuccess = array(
"message" => "Úspech! Skontrolujte si prosím Vašu e-mailovú schránku."
);
echo json_encode($dataSuccess);
}
mysqli_close($emaildb);
?>
My newuser.php:
require 'config.php';
use PHPMailer\PHPMailer\PHPMailer;
date_default_timezone_set('Europe/Bratislava');
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.websupport.sk';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'test@true-blue.sk';
$mail->Password = 'pass';
$mail->setFrom('test@true-blue.sk', 'Fesky Vanka');
$mail->addReplyTo('test@true-blue.sk', 'Fesky Vanka');
$mail->addAddress('rebros.matus@gmail.com', 'John Doe');
$mail->Subject = 'Skuska skusiek';
$mail->msgHTML(file_get_contents('newmail.html'), __DIR__);
//$mail->AltBody = 'Na Váš email odteraz budu chodiť moje dokumenty.';
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
}
My script.js:
(function($) {
var form = $('#email-form');
var message = $('#message');
form.on('submit', function(event) {
event.preventDefault();
$.ajax({
url: form.attr('action'),
type: 'POST',
data: form.serialize()
}).done(function( res ) {
console.log( res );
form.hide();
$('#email-text').hide();
if (res.existing) message.addClass('duplicate');
else message.addClass('success');
message.append(res.message);
$.ajax({
url: 'documents.php',
type: 'GET'
}).done(function ( res ) {
$('#docss').append(res);
console.log( res );
}).fail(function( res ) {
$('#docss').append(res);
console.error( res );
});
}).fail(function( res ) {
console.log( res );
alert('Prosím zadajte váš email');
});
});
}(jQuery));