email = Services::email($emailConfig); } /** * Kirim OTP via Email * * @param string $toEmail Email tujuan * @param string $otpCode Kode OTP * @param string $userName Nama user * @param string $pangkat Pangkat user * @return array ['success' => bool, 'message' => string] */ public function sendOtpEmail($toEmail, $otpCode, $userName, $pangkat = '') { try { $this->email->setFrom('prodevbackend587@gmail.com', 'E-Teguran Humanis Polda NTB'); $this->email->setTo($toEmail); $this->email->setSubject('E-Teguran Humanis - Verifikasi OTP'); $message = $this->getOtpEmailTemplate($otpCode, $userName, $pangkat); $this->email->setMessage($message); $result = $this->email->send(); if ($result) { // PATCH: Set SMTPConnect ke null agar tidak error di __destruct $reflection = new \ReflectionProperty($this->email, 'SMTPConnect'); $reflection->setAccessible(true); $reflection->setValue($this->email, null); // Cleanup $this->email->clear(true); return [ 'success' => true, 'message' => 'Email OTP berhasil dikirim' ]; } else { log_message('error', 'Email send error: ' . $this->email->printDebugger(['headers'])); // PATCH: Set SMTPConnect ke null agar tidak error di __destruct $reflection = new \ReflectionProperty($this->email, 'SMTPConnect'); $reflection->setAccessible(true); $reflection->setValue($this->email, null); // Cleanup $this->email->clear(true); return [ 'success' => false, 'message' => 'Gagal mengirim email OTP' ]; } } catch (\Exception $e) { log_message('error', 'Email exception: ' . $e->getMessage()); // PATCH: Set SMTPConnect ke null agar tidak error di __destruct try { $reflection = new \ReflectionProperty($this->email, 'SMTPConnect'); $reflection->setAccessible(true); $reflection->setValue($this->email, null); $this->email->clear(true); } catch (\Exception $cleanupEx) { // Ignore cleanup errors } return [ 'success' => false, 'message' => 'Terjadi kesalahan saat mengirim email: ' . $e->getMessage() ]; } } /** * Template HTML untuk email OTP (sesuai desain aplikasi) */ private function getOtpEmailTemplate($otpCode, $userName, $pangkat = '') { $fullName = !empty($pangkat) ? "{$pangkat} {$userName}" : $userName; return ' E-Teguran Humanis Polda NTB
'; } }