feat(webstatement): tambahkan pengiriman email dengan fallback konfigurasi
- **Implementasi Custom Email Sender:**
- Menambahkan metode `send()` dengan EsmtpTransport untuk mendukung pengiriman email menggunakan fallback konfigurasi yang lebih fleksibel.
- Penanganan port `STARTTLS` dengan koneksi manual serta pengaturan SSL.
- **Refaktor Method Build:**
- Memperbaiki tampilan struktur email:
- Menyertakan template email, attachment, dan penempatan properti email secara lebih dinamis.
- Mendukung file PDF atau ZIP sebagai lampiran.
- **Implementasi Konversi Laravel to Symfony Mailer:**
- Metode `toSymfonyEmail()` diimplementasikan untuk mengonversi email Laravel ke Symfony Mailer.
- Penanganan dari email `from`, `to`, `subject`, hingga body HTML secara langsung.
- Penambahan mekanisme attachment dengan validasi eksistensi file.
- **Peningkatan Logging:**
- Menambahkan logging detail untuk setiap tahap proses pengiriman, termasuk metode fallback yang berhasil atau gagal.
- Log peringatan dan error ditambahkan saat terjadi kegagalan di setiap metode yang dicoba.
- **Penanganan Error:**
- Menangani pengecualian dan pencatatan error terakhir dari koneksi email manual serta fallback ke Laravel mailer jika cara custom gagal.
- **Konsistensi Format:**
- Melakukan perapihan atribut class serta alignment pada kode untuk meningkatkan keterbacaan dan konsistensi.
- **Optimalisasi Email Statement:**
- Memastikan format periode (bulanan/range) tampil dinamis pada subjek email.
- Menambahkan validasi serta pengelolaan untuk lampiran file sebelum pengiriman.
Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user