statement = $statement; $this->filePath = $filePath; $this->isZip = $isZip; $this->phpMailerService = new PHPMailerService(); } /** * Kirim email statement * * @param string $emailAddress * @return bool */ public function send(string $emailAddress): bool { try { // Generate subject $subject = $this->generateSubject(); // Generate email body $body = $this->generateEmailBody(); // Generate attachment name $attachmentName = $this->generateAttachmentName(); // Determine MIME type $mimeType = $this->isZip ? 'application/zip' : 'application/pdf'; // Send email using PHPMailer $result = $this->phpMailerService->sendEmail( $emailAddress, $subject, $body, $this->filePath, $attachmentName, $mimeType ); Log::info('Statement email sent via PHPMailer', [ 'to' => $emailAddress, 'subject' => $subject, 'attachment' => $attachmentName, 'account_number' => $this->statement->account_number, 'period' => $this->statement->period_from, 'success' => $result ]); return $result; } catch (\Exception $e) { Log::error('Failed to send statement email via PHPMailer', [ 'to' => $emailAddress, 'account_number' => $this->statement->account_number, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } /** * Generate email subject * * @return string */ protected function generateSubject(): string { $subject = 'Statement Rekening Bank Artha Graha Internasional'; if ($this->statement->is_period_range) { $subject .= " - {$this->statement->period_from} to {$this->statement->period_to}"; } else { $subject .= " - " . \Carbon\Carbon::createFromFormat('Ym', $this->statement->period_from)->locale('id')->isoFormat('MMMM Y'); } // Add batch info for batch requests if ($this->statement->request_type && $this->statement->request_type !== 'single_account') { $subject .= " [{$this->statement->request_type}]"; } if ($this->statement->batch_id) { $subject .= " [Batch: {$this->statement->batch_id}]"; } return $subject; } /** * Generate email body HTML * * @return string */ protected function generateEmailBody(): string { try { // Get account data $account = Account::where('account_number', $this->statement->account_number)->first(); // Prepare data for view $data = [ 'statement' => $this->statement, 'accountNumber' => $this->statement->account_number, 'periodFrom' => $this->statement->period_from, 'periodTo' => $this->statement->period_to, 'isRange' => $this->statement->is_period_range, 'requestType' => $this->statement->request_type, 'batchId' => $this->statement->batch_id, 'accounts' => $account ]; // Render view to HTML return View::make('webstatement::statements.email', $data)->render(); } catch (\Exception $e) { Log::error('Failed to generate email body', [ 'account_number' => $this->statement->account_number, 'error' => $e->getMessage() ]); // Fallback to simple HTML return $this->generateFallbackEmailBody(); } } /** * Generate fallback email body * * @return string */ protected function generateFallbackEmailBody(): string { $periodText = $this->statement->is_period_range ? "periode {$this->statement->period_from} sampai {$this->statement->period_to}" : "periode " . \Carbon\Carbon::createFromFormat('Ym', $this->statement->period_from)->locale('id')->isoFormat('MMMM Y'); return "
Yth. Nasabah,
Terlampir adalah statement rekening Anda untuk {$periodText}.
Nomor Rekening: {$this->statement->account_number}
Terima kasih atas kepercayaan Anda.
Salam,
Bank Artha Graha Internasional