false, 'message' => '알 수 없는 오류가 발생했습니다.' ]; try { // 템플릿 코드는 필수입니다. if (empty($template_code)) { throw new Exception('메일 발송에 필요한 템플릿 코드가 지정되지 않았습니다.'); } // 수신자 이메일($to_email)이 지정되지 않은 경우, 사이트의 최고 관리자에게 발송합니다. // (예: 홈페이지 문의 양식) if (empty($to_email)) { global $config; $to_email = $config['cf_admin_email']; if (empty($to_email)) { throw new Exception('수신자 이메일이 지정되지 않았고, 사이트 기본 관리자 이메일도 설정되지 않았습니다.'); } } // [추가] 클라이언트에서 'ADMIN_EMAIL'이라는 특별한 값을 보내면, // 서버 설정(config.php)에 저장된 실제 관리자 이메일로 치환합니다. // 이렇게 하면 클라이언트 JS 소스코드에 관리자 이메일이 노출되지 않습니다. global $config; $admin_email = $config['cf_admin_email']; // cc_email 처리 if (!empty($cc_email)) { if (is_array($cc_email)) { foreach ($cc_email as $key => $email) { if ($email === 'ADMIN_EMAIL') { $cc_email[$key] = $admin_email; } } } else if ($cc_email === 'ADMIN_EMAIL') { $cc_email = $admin_email; } } // bcc_email 처리 if (!empty($bcc_email)) { if (is_array($bcc_email)) { foreach ($bcc_email as $key => $email) { if ($email === 'ADMIN_EMAIL') { $bcc_email[$key] = $admin_email; } } } else if ($bcc_email === 'ADMIN_EMAIL') { $bcc_email = $admin_email; } } // --- 메일 발송 실행 --- $mailSender = new MailSender(); // send 메서드 시그니처 변경에 맞춰 인자 전달 $success = $mailSender->send($template_code, $to_email, $vars, $cc_email, $bcc_email); if ($success) { $response['success'] = true; $response['message'] = '요청하신 메일이 성공적으로 발송되었습니다.'; } else { // MailSender 클래스 내부에서 이미 로그를 기록하므로, 사용자에게는 간단한 메시지만 전달합니다. throw new Exception('메일 발송에 실패했습니다. 시스템 로그를 확인해주세요.'); } } catch (Exception $e) { // 예외 발생 시 에러 메시지를 응답에 담습니다. $response['message'] = $e->getMessage(); } // 최종 결과를 JSON 형태로 출력합니다. echo json_encode($response); exit;