Compare commits
4 Commits
9199a4d748
...
b717749450
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b717749450 | ||
|
|
e5b8dfc7c4 | ||
|
|
d5482fb824 | ||
|
|
f6df453ddc |
@@ -5,10 +5,11 @@ namespace Modules\Webstatement\Console;
|
||||
use Exception;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Basicdata\Models\Branch;
|
||||
use Modules\Webstatement\Jobs\SendStatementEmailJob;
|
||||
use Modules\Webstatement\Models\Account;
|
||||
use Modules\Webstatement\Models\PrintStatementLog;
|
||||
use Modules\Basicdata\Models\Branch;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Command untuk mengirim email statement PDF ke nasabah
|
||||
@@ -182,7 +183,7 @@ class SendStatementEmailCommand extends Command
|
||||
case 'all':
|
||||
return ['all_branches', null];
|
||||
default:
|
||||
throw new \InvalidArgumentException("Invalid type: {$type}");
|
||||
throw new InvalidArgumentException("Invalid type: {$type}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Modules\Webstatement\Jobs;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
@@ -11,10 +12,11 @@ use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use InvalidArgumentException;
|
||||
use Modules\Webstatement\Mail\StatementEmail;
|
||||
use Modules\Webstatement\Models\Account;
|
||||
use Modules\Webstatement\Models\PrintStatementLog;
|
||||
use Modules\Webstatement\Mail\StatementEmail;
|
||||
use Modules\Basicdata\Models\Branch;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Job untuk mengirim email PDF statement ke nasabah
|
||||
@@ -59,7 +61,8 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
/**
|
||||
* Menjalankan job pengiriman email statement
|
||||
*/
|
||||
public function handle(): void
|
||||
public function handle()
|
||||
: void
|
||||
{
|
||||
Log::info('Starting SendStatementEmailJob execution', [
|
||||
'batch_id' => $this->batchId,
|
||||
@@ -118,7 +121,7 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
'email' => $this->getEmailForAccount($account),
|
||||
'batch_id' => $this->batchId
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$failedCount++;
|
||||
|
||||
Log::error('Failed to send statement email', [
|
||||
@@ -161,7 +164,7 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
'final_status' => $finalStatus
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
DB::rollBack();
|
||||
|
||||
$this->updateLogStatus('failed', [
|
||||
@@ -179,6 +182,27 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status log
|
||||
*/
|
||||
private function updateLogStatus($status, $additionalData = [])
|
||||
{
|
||||
if (!$this->logId) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$updateData = array_merge(['status' => $status], $additionalData);
|
||||
PrintStatementLog::where('id', $this->logId)->update($updateData);
|
||||
} catch (Exception $e) {
|
||||
Log::error('Failed to update log status', [
|
||||
'log_id' => $this->logId,
|
||||
'status' => $status,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mengambil accounts berdasarkan request type
|
||||
*/
|
||||
@@ -211,7 +235,7 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \InvalidArgumentException("Invalid request type: {$this->requestType}");
|
||||
throw new InvalidArgumentException("Invalid request type: {$this->requestType}");
|
||||
}
|
||||
|
||||
$accounts = $query->get();
|
||||
@@ -233,30 +257,64 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status log
|
||||
* Mengirim email statement untuk account tertentu
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function updateLogStatus($status, $additionalData = [])
|
||||
private function sendStatementEmail(Account $account)
|
||||
{
|
||||
if (!$this->logId) {
|
||||
return;
|
||||
// Dapatkan email untuk pengiriman
|
||||
$emailAddress = $this->getEmailForAccount($account);
|
||||
|
||||
if (!$emailAddress) {
|
||||
throw new Exception("No email address found for account {$account->account_number}");
|
||||
}
|
||||
|
||||
try {
|
||||
$updateData = array_merge(['status' => $status], $additionalData);
|
||||
PrintStatementLog::where('id', $this->logId)->update($updateData);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Failed to update log status', [
|
||||
'log_id' => $this->logId,
|
||||
'status' => $status,
|
||||
'error' => $e->getMessage()
|
||||
]);
|
||||
// Cek apakah file PDF ada
|
||||
$pdfPath = $this->getPdfPath($account->account_number, $account->branch_code);
|
||||
|
||||
if (!Storage::exists($pdfPath)) {
|
||||
throw new Exception("PDF file not found: {$pdfPath}");
|
||||
}
|
||||
|
||||
// Buat atau update log statement
|
||||
$statementLog = $this->createOrUpdateStatementLog($account);
|
||||
|
||||
// Dapatkan path absolut file
|
||||
$absolutePdfPath = Storage::path($pdfPath);
|
||||
|
||||
// Kirim email
|
||||
// Add delay between email sends to prevent rate limiting
|
||||
sleep(1); // 2 second delay
|
||||
Mail::to($emailAddress)->send(
|
||||
new StatementEmail($statementLog, $absolutePdfPath, false)
|
||||
);
|
||||
|
||||
// Update status log dengan email yang digunakan
|
||||
$statementLog->update([
|
||||
'email_sent_at' => now(),
|
||||
'email_status' => 'sent',
|
||||
'email_address' => $emailAddress // Simpan email yang digunakan untuk tracking
|
||||
]);
|
||||
|
||||
Log::info('Email sent for account', [
|
||||
'account_number' => $account->account_number,
|
||||
'branch_code' => $account->branch_code,
|
||||
'email' => $emailAddress,
|
||||
'email_source' => !empty($account->stmt_email) ? 'account.stmt_email' : 'customer.email',
|
||||
'pdf_path' => $pdfPath,
|
||||
'batch_id' => $this->batchId
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mendapatkan email untuk pengiriman statement
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getEmailForAccount(Account $account)
|
||||
@@ -291,64 +349,12 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mengirim email statement untuk account tertentu
|
||||
*
|
||||
* @param Account $account
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function sendStatementEmail(Account $account)
|
||||
{
|
||||
// Dapatkan email untuk pengiriman
|
||||
$emailAddress = $this->getEmailForAccount($account);
|
||||
|
||||
if (!$emailAddress) {
|
||||
throw new \Exception("No email address found for account {$account->account_number}");
|
||||
}
|
||||
|
||||
// Cek apakah file PDF ada
|
||||
$pdfPath = $this->getPdfPath($account->account_number, $account->branch_code);
|
||||
|
||||
if (!Storage::exists($pdfPath)) {
|
||||
throw new \Exception("PDF file not found: {$pdfPath}");
|
||||
}
|
||||
|
||||
// Buat atau update log statement
|
||||
$statementLog = $this->createOrUpdateStatementLog($account);
|
||||
|
||||
// Dapatkan path absolut file
|
||||
$absolutePdfPath = Storage::path($pdfPath);
|
||||
|
||||
// Kirim email
|
||||
// Add delay between email sends to prevent rate limiting
|
||||
sleep(1); // 2 second delay
|
||||
Mail::to($emailAddress)->send(
|
||||
new StatementEmail($statementLog, $absolutePdfPath, false)
|
||||
);
|
||||
|
||||
// Update status log dengan email yang digunakan
|
||||
$statementLog->update([
|
||||
'email_sent_at' => now(),
|
||||
'email_status' => 'sent',
|
||||
'email_address' => $emailAddress // Simpan email yang digunakan untuk tracking
|
||||
]);
|
||||
|
||||
Log::info('Email sent for account', [
|
||||
'account_number' => $account->account_number,
|
||||
'branch_code' => $account->branch_code,
|
||||
'email' => $emailAddress,
|
||||
'email_source' => !empty($account->stmt_email) ? 'account.stmt_email' : 'customer.email',
|
||||
'pdf_path' => $pdfPath,
|
||||
'batch_id' => $this->batchId
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mendapatkan path file PDF statement
|
||||
*
|
||||
* @param string $accountNumber
|
||||
* @param string $branchCode
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getPdfPath($accountNumber, $branchCode)
|
||||
@@ -360,6 +366,7 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
* Membuat atau update log statement
|
||||
*
|
||||
* @param Account $account
|
||||
*
|
||||
* @return PrintStatementLog
|
||||
*/
|
||||
private function createOrUpdateStatementLog(Account $account)
|
||||
@@ -399,7 +406,7 @@ class SendStatementEmailJob implements ShouldQueue
|
||||
/**
|
||||
* Handle job failure
|
||||
*/
|
||||
public function failed(\Throwable $exception)
|
||||
public function failed(Throwable $exception)
|
||||
{
|
||||
$this->updateLogStatus('failed', [
|
||||
'completed_at' => now(),
|
||||
|
||||
@@ -2,11 +2,18 @@
|
||||
|
||||
namespace Modules\Webstatement\Mail;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Exception;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Mail\Mailable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Log;
|
||||
use Modules\Webstatement\Models\Account;
|
||||
use Modules\Webstatement\Models\PrintStatementLog;
|
||||
use Symfony\Component\Mailer\Mailer;
|
||||
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
|
||||
use Symfony\Component\Mime\Email;
|
||||
|
||||
class StatementEmail extends Mailable
|
||||
{
|
||||
@@ -15,6 +22,7 @@ class StatementEmail extends Mailable
|
||||
protected $statement;
|
||||
protected $filePath;
|
||||
protected $isZip;
|
||||
protected $message;
|
||||
|
||||
/**
|
||||
* Create a new message instance.
|
||||
@@ -31,6 +39,92 @@ class StatementEmail extends Mailable
|
||||
$this->isZip = $isZip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override the send method to use EsmtpTransport directly
|
||||
* Using the working configuration from Python script with multiple fallback methods
|
||||
*/
|
||||
public function send($mailer)
|
||||
{
|
||||
// Get mail configuration
|
||||
$host = Config::get('mail.mailers.smtp.host');
|
||||
$port = Config::get('mail.mailers.smtp.port');
|
||||
$username = Config::get('mail.mailers.smtp.username');
|
||||
$password = Config::get('mail.mailers.smtp.password');
|
||||
|
||||
Log::info('StatementEmail: Attempting to send email with multiple fallback methods');
|
||||
|
||||
// Define connection methods like in Python script
|
||||
$method =
|
||||
// Method 3: STARTTLS with original port
|
||||
[
|
||||
'port' => $port,
|
||||
'ssl' => false,
|
||||
'name' => 'STARTTLS (Port $port)'
|
||||
];
|
||||
|
||||
$lastException = null;
|
||||
|
||||
// Try each connection method until one succeeds
|
||||
try {
|
||||
Log::info('StatementEmail: Trying ' . $method['name']);
|
||||
|
||||
// Create EsmtpTransport with current method
|
||||
$transport = new EsmtpTransport($host, $method['port'], $method['ssl']);
|
||||
|
||||
// Set username and password
|
||||
if ($username) {
|
||||
$transport->setUsername($username);
|
||||
}
|
||||
if ($password) {
|
||||
$transport->setPassword($password);
|
||||
}
|
||||
|
||||
// Disable SSL verification for development
|
||||
$streamOptions = [
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
'allow_self_signed' => true
|
||||
]
|
||||
];
|
||||
$transport->getStream()->setStreamOptions($streamOptions);
|
||||
|
||||
// Build the email content
|
||||
$this->build();
|
||||
|
||||
// Start transport connection
|
||||
$transport->start();
|
||||
|
||||
// Create Symfony mailer
|
||||
$symfonyMailer = new Mailer($transport);
|
||||
|
||||
// Convert Laravel message to Symfony Email
|
||||
$email = $this->toSymfonyEmail();
|
||||
|
||||
// Send the email
|
||||
$symfonyMailer->send($email);
|
||||
|
||||
// Close connection
|
||||
$transport->stop();
|
||||
|
||||
Log::info('StatementEmail: Successfully sent email using ' . $method['name']);
|
||||
return $this;
|
||||
|
||||
} catch (Exception $e) {
|
||||
$lastException = $e;
|
||||
Log::warning('StatementEmail: Failed to send with ' . $method['name'] . ': ' . $e->getMessage());
|
||||
// Continue to next method
|
||||
}
|
||||
|
||||
try {
|
||||
return parent::send($mailer);
|
||||
} catch (Exception $e) {
|
||||
Log::error('StatementEmail: Laravel mailer also failed: ' . $e->getMessage());
|
||||
// If we got here, throw the last exception from our custom methods
|
||||
throw $lastException;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the message.
|
||||
* Membangun struktur email dengan attachment statement
|
||||
@@ -44,12 +138,44 @@ class StatementEmail extends Mailable
|
||||
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');
|
||||
$subject .= " - " . Carbon::createFromFormat('Ym', $this->statement->period_from)
|
||||
->locale('id')
|
||||
->isoFormat('MMMM Y');
|
||||
}
|
||||
|
||||
$email = $this->subject($subject)
|
||||
->view('webstatement::statements.email')
|
||||
->with([
|
||||
$email = $this->subject($subject);
|
||||
|
||||
// Store the email in the message property for later use in toSymfonyEmail()
|
||||
$this->message = $email;
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Laravel message to Symfony Email
|
||||
*/
|
||||
protected function toSymfonyEmail()
|
||||
{
|
||||
// Build the message if it hasn't been built yet
|
||||
$this->build();
|
||||
// Create a new Symfony Email
|
||||
$email = new Email();
|
||||
|
||||
// Set from address using config values instead of trying to call getFrom()
|
||||
$fromAddress = Config::get('mail.from.address');
|
||||
$fromName = Config::get('mail.from.name');
|
||||
$email->from($fromName ? "$fromName <$fromAddress>" : $fromAddress);
|
||||
|
||||
// Set to addresses - use the to addresses from the mailer instead of trying to call getTo()
|
||||
// We'll get the to addresses from the Mail facade when the email is sent
|
||||
// For now, we'll just add a placeholder recipient that will be overridden by the Mail facade
|
||||
$email->to($this->message->to[0]['address']);
|
||||
|
||||
$email->subject($this->message->subject);
|
||||
|
||||
// Set body - use a simple HTML content instead of trying to call getHtmlBody()
|
||||
// In a real implementation, we would need to find a way to access the rendered HTML content
|
||||
$email->html(view('webstatement::statements.email', [
|
||||
'statement' => $this->statement,
|
||||
'accountNumber' => $this->statement->account_number,
|
||||
'periodFrom' => $this->statement->period_from,
|
||||
@@ -58,20 +184,19 @@ class StatementEmail extends Mailable
|
||||
'requestType' => $this->statement->request_type,
|
||||
'batchId' => $this->statement->batch_id,
|
||||
'accounts' => Account::where('account_number', $this->statement->account_number)->first()
|
||||
]);
|
||||
])->render());
|
||||
//$email->text($this->message->getTextBody());
|
||||
|
||||
// Add attachments - use the file path directly instead of trying to call getAttachments()
|
||||
if ($this->filePath && file_exists($this->filePath)) {
|
||||
if ($this->isZip) {
|
||||
$fileName = "{$this->statement->account_number}_{$this->statement->period_from}_to_{$this->statement->period_to}.zip";
|
||||
$email->attach($this->filePath, [
|
||||
'as' => $fileName,
|
||||
'mime' => 'application/zip',
|
||||
]);
|
||||
$contentType = 'application/zip';
|
||||
} else {
|
||||
$fileName = "{$this->statement->account_number}_{$this->statement->period_from}.pdf";
|
||||
$email->attach($this->filePath, [
|
||||
'as' => $fileName,
|
||||
'mime' => 'application/pdf',
|
||||
]);
|
||||
$contentType = 'application/pdf';
|
||||
}
|
||||
$email->attachFromPath($this->filePath, $fileName, $contentType);
|
||||
}
|
||||
|
||||
return $email;
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 90%;
|
||||
margin: 20px auto;
|
||||
max-width: 100%;
|
||||
margin: 0px auto;
|
||||
background-color: #ffffff;
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
@@ -37,7 +37,7 @@
|
||||
}
|
||||
|
||||
.content {
|
||||
padding: 30px;
|
||||
padding: 5px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
@@ -103,11 +103,7 @@
|
||||
Terima Kasih,<br><br>
|
||||
|
||||
<strong>Bank Artha Graha Internasional</strong><br>
|
||||
------------------------------
|
||||
<wbr>
|
||||
------------------------------
|
||||
<wbr>
|
||||
--------<br>
|
||||
------------------------------------------------------------<br>
|
||||
Kami sangat menghargai masukan dan saran Anda untuk meningkatkan layanan dan produk kami.<br>
|
||||
Untuk memberikan masukan, silakan hubungi <strong>GrahaCall 24 Jam</strong> kami di
|
||||
<strong>0-800-191-8880</strong>.<br><br><br>
|
||||
@@ -132,11 +128,7 @@
|
||||
Regards,<br><br>
|
||||
|
||||
<strong>Bank Artha Graha Internasional</strong><br>
|
||||
------------------------------
|
||||
<wbr>
|
||||
------------------------------
|
||||
<wbr>
|
||||
--------<br>
|
||||
------------------------------------------------------------<br>
|
||||
We welcome any feedback or suggestions to improve our product and services.<br>
|
||||
If you have any feedback, please contact our <strong>GrahaCall 24 Hours</strong> at
|
||||
<strong>0-800-191-8880</strong>.
|
||||
@@ -145,10 +137,6 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<p>© {{ date('Y') }} Bank Artha Graha Internasional. All rights reserved.</p>
|
||||
<p>Jika Anda memiliki pertanyaan, silakan hubungi customer service kami.</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user