sendTestMail($outgoingServerSettingsModel); if ($sent) { $data = $outgoingServerSettingsModel->getData(); $data['message'] = '✅ Configuration saved and test mail sent successfully.'; $data['success'] = true; $response->setResult($data); } else { $response->setResult([ 'success' => false, 'message' => '⚠️ Test mail failed to send. Check SMTP settings or logs.' ]); } } catch (Exception $e) { $response->setError($e->getCode(), $e->getMessage()); } // Important: emit and stop ALL further output $response->emit(); exit; // 🔹 This prevents extra output after JSON } public function sendTestMail($model , $subject = null, $body = null,$pdf_path = null) { require_once 'vtlib/Vtiger/Mailer.php'; $mailer = new Vtiger_Mailer(); try { $mailer->IsSMTP(); $mailer->Host = $model->get('server'); $mailer->Port = 587; $mailer->SMTPAuth = $model->isSmtpAuthEnabled(); $mailer->Username = $model->get('server_username'); $mailer->Password = $model->get('server_password'); $mailer->SMTPSecure = 'tls'; $mailer->From = $model->get('from_email_field'); $mailer->FromName = 'Vtiger Test Mail'; $mailer->AddAddress($model->to_email ?? 'souldibachir3150@gmail.com'); $mailer->AddCC('andryamo2231@gmail.com'); $mailer->Subject = $subject ?? 'Test Mail from Vtiger CRM 2'; $mailer->IsHTML(true); $mailer->Body = $body ?? 'This is a test mail sent when saving outgoing server configuration. 2'.$pdf_path; // // Attach Sales Order PDF if available // if (!empty($model->pdf_path) && file_exists($model->pdf_path)) { // $mailer->AddAttachment($model->pdf_path, basename($model->pdf_path)); // } if (!$mailer->Send(true)) { error_log('SMTP send() failed: ' . $mailer->ErrorInfo); return false; } return true; } catch (Exception $e) { error_log('SMTP Test Mail Error: ' . $e->getMessage()); return false; } } }