I have really big problem with this json_encode which need use inside foreach loop which is inside if statement. However, if I need to send mail just to one person, json_encode is very easy, but the problem is because I need to get email addresses from database and so I first check if mail count is greater than zero and later use foreach loop for send mail to specified db query. So, for properly encode this php array of $data I think that this need stay outside of if mail count is greater than zero. Otherwise, I get error in console AJAX error:parseerror and Syntax error: Unexpected end of JSON input. Perhaps, if this statement of php array $data it's outside of statement mail count is greater than zero then json_encode work very well. When a form is submitted I get correct response on the web page, in console and in network tab, but email isn't sent. However, if I comment js file, when a form is submitted, email is sent correctly on every page, all data it's inserted into db and perhaps this content of $data it's displayed successfully, but I need AJAX for this, so please can you check this my php file and try to help me, because I can not figure out more where is the error and how to fix it. I was read complete google, but dont exist even 1 exemple of how to use json when send mail to multiple recipients, or when get recipients from db etc. I can not figure out more how to fix this problem. Perhaps, I think too that this statement if($error==false) need stay inside if(mailOwner->sent), but in this case how to write else?! So complicated for me, that can not understand anything more. So, this is complete code of php file.
<?php
/*ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
// define variables and set to empty values
$fname = $tel = $userMail = $userMessage = $email_address_id ="";
$fname_error = $tel_error = $userMail_error = $userMessage_error = "";
$error = false;
//Load the config file
$dbHost = "localhost";
$dbUser = "secret";
$dbPassword = "secret";
$dbName = "visitneum";
$dbCharset = "utf8";
try{
$dsn = "mysql:host=" . $dbHost . ";dbName=" . $dbName . ";charset=" . $dbCharset;
$pdo = new PDO($dsn, $dbUser, $dbPassword);
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
//echo "connected!";
}catch(PDOException $e){
echo "Greška u konekciji: " . $e->getMessage();
}
use PHPMailer\PHPMailer\PHPMailer;
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
require 'PHPMailer/Exception.php';
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if (isset($_POST['submitOwner'])) {
$fname = $_POST['fname'];
$tel = $_POST['tel'];
$userMail = $_POST["userMail"];
$userMessage = $_POST["userMessage"];
if(empty($_POST["fname"])) {
$error=true;
$fname_error = "Ime i prezime ne može biti prazno!";
}else{
$fname = $_POST['fname'];
if(!preg_match("/^[a-zšđčćžA-ZŠĐČĆŽ\s]*$/", $fname)){
$fname_error = "Ime i prezime mogu da sadrže samo slova i razmak!";
}
}
if(empty($_POST["tel"])) {
$tel_error = "Broj telefona ne može biti prazan!";
}else{
$tel = $_POST['tel'];
if(!preg_match('/^[\+]?[0-9]{9,15}$/', $tel)) {
$tel_error = "Broj telefona treba da sadrži minimalno od 9 do 15 brojeva!";
}
}
if(empty($_POST["userMail"])){
$userMail_error = "E-mail ne može biti prazan!";
}else{
$userMail = $_POST["userMail"];
// check if e-mail address is well-formed
if(!filter_var($userMail, FILTER_VALIDATE_EMAIL)) {
$userMail_error = "E-mail adresa nije ispravna!";
}
}
if(empty($_POST["userMessage"])) {
$userMessage_error = "Sadržaj poruke ne može biti prazan!";
}else{
$userMessage = $_POST["userMessage"];
if(!preg_match("/^[a-zšđčćžA-ZŠĐČĆŽ0-9 ,.!?\'\"]*$/", $userMessage)){
$userMessage_error = "Sadržaj poruke ne mogu biti specijalni znaci!";
}
}
if($fname_error == '' && $tel_error == '' && $userMail_error == '' && $userMessage_error == ''){
// Instantiate a NEW email
$mailOwner = new PHPMailer(true);
$mailOwner->CharSet = "UTF-8";
//SMTP Settings
$mailOwner->isSMTP();
$mailOwner->Host = 'mail.visit-neum.com';
$mailOwner->SMTPAuth = true;
//$mailOwner->SMTPDebug = 2;
$mailOwner->Username = 'secret';
$mailOwner->Password = 'secret';
$mailOwner->Port = 465; // 587
$mailOwner->SMTPSecure = 'ssl'; // tls
$mailOwner->WordWrap = 50;
$mailOwner->isHTML(true);
$mailOwner->setFrom('secret');
$mailOwner->clearAddresses();
$mailOwner->smtpClose();
$mailOwner->Subject = "Nova poruka od visit-neum.com";
// SELECT email values from database for man owners of others property(restaurants, ships, etc)
$query_m = "SELECT owners_email.email_address_id, email_address, owner_name, owner_property, owner_sex,
owner_type FROM visitneum.owners_email INNER JOIN visitneum.pages ON (pages.email_address_id = owners_email.email_address_id) WHERE `owner_sex`='M' AND `owner_type`='other' AND `pages_id` = ?";
$dbstmt = $pdo->prepare($query_m);
$dbstmt->bindParam(1,$pages_id);
$dbstmt->execute();
//var_dump($dbstmt);
//get emails from db via pdo
$emails_other = $dbstmt->fetchAll(PDO::FETCH_ASSOC);
if(is_array($emails_other) && count($emails_other) > 0) {
foreach ($emails_other as $email_other){
//var_dump($email_other['email_address']);
$mailOwner->addAddress($email_other['email_address']);
$body_other = "<p>Poštovani {$email_other['owner_name']}, <br>" . "Upravo ste primili poruku sa sajta <a href='https://www.visit-neum.com'>visit-neum.com</a><br>Detalji Vaše poruke se nalaze ispod:</p><p><strong>Od: </strong>" . ucwords($fname) . "<br><strong>Telefon: </strong>" . $tel . "<br><strong>E-mail: </strong>" .strtolower($userMail)."<br><strong>Poruka: </strong>" . $userMessage . "<br><br><strong>Napomena: </strong>Molimo Vas da na ovu poruku ne odgovarate. Vaš odgovor pošaljite na: " . strtoupper($userMail) . "</p>";
$mailOwner->Body = $body_other;
if($mailOwner->send()){
$mailOwner = "INSERT INTO visitneum.contact_owner (fname, tel, userMail, userMessage, email_address_id) VALUES (:fname, :tel, :userMail, :userMessage, :email_address_id)";
$stmt = $pdo->prepare($mailOwner);
$stmt->execute(['fname' => $fname, 'tel' => $tel, 'userMail' => $userMail, 'userMessage' => $userMessage, 'email_address_id' => $email_other['email_address_id']]);
}//end if mail send
}//end foreach for email addresses (man owners of other properties(restaurants, ships etc.))
}//end if for array of emails
if($error==false){
$data['response'] = "success";
$data['content'] = "Hvala Vam " . ucwords($fname) . "! Vaša poruka je uspješno poslata vlasniku objekta! Odgovor ćete dobiti ubrzo!";
$jsonData = $data;
}//end if $error==false*/
else{
$data['response'] = "error";
$data['content'] = "Došlo je do greške! Pokušajte ponovo..." . $mailOwner->ErrorInfo;
$jsonData = $data;
}
//header("Content-Type: application/json; charset=utf-8", true);
echo json_encode($jsonData);
exit();
}//end if validation
}//end submitOwner
}//end REQUEST METHOD = POST
Any kind of help will be highly appreciated and thanks in advance.
Aucun commentaire:
Enregistrer un commentaire