## Getting Started
### Minimal Setup
1. **Installation**
Add the bundle via Composer:
```bash
composer require austral/email-bundle
Register the bundle in config/bundles.php:
Austral\EmailBundle\AustralEmailBundle::class => ['all' => true],
Configuration Publish the default config:
php artisan vendor:publish --tag=email-bundle-config
Update config/austral_email.php with your email settings (SMTP, Mailgun, etc.).
First Use Case: Sending a Template Email
Define a template in resources/views/emails/welcome.blade.php:
<h1>Welcome, {{ $name }}!</h1>
<p>Your account has been created.</p>
Send the email in a controller:
use Austral\EmailBundle\Service\EmailService;
public function sendWelcomeEmail(EmailService $emailService)
{
$emailService->send([
'to' => 'user@example.com',
'subject' => 'Welcome!',
'template' => 'emails.welcome',
'data' => ['name' => 'John Doe'],
]);
}
Viewing Sent Emails
Access the email log via the admin panel (if configured) or query the email_log table directly:
$sentEmails = \Austral\EmailBundle\Entity\EmailLog::all();
Template-Based Emails
resources/views/emails/.data array in EmailService::send().$emailService->send([
'to' => ['user1@example.com', 'user2@example.com'],
'cc' => 'manager@example.com',
'bcc' => 'archive@example.com',
'subject' => 'Your Order #{{ $orderId }}',
'template' => 'emails.order_confirmation',
'data' => ['orderId' => 12345, 'total' => '$99.99'],
]);
Email Logging
email_log table (includes to, subject, sent_at, status).EmailLog entity to add custom fields (e.g., template_name, metadata):
// In a migration or entity listener
$emailLog->setTemplate('emails.order_confirmation');
$emailLog->setMetadata(['order_id' => 12345]);
Queueing Emails
$emailService->sendQueued([...]); // Adds to queue:emails
php artisan queue:work
Admin Panel Integration
EmailLog entity for CRUD operations:
# config/austral_admin.yaml
entities:
Austral\EmailBundle\Entity\EmailLog: ~
Localization
entity-translate-bundle for multilingual emails:
$emailService->send([
'locale' => 'es', // Override default locale
'template' => 'emails.welcome',
'data' => ['name' => 'Juan'],
]);
Customizing Email Headers Override default headers in the config:
// config/austral_email.php
'headers' => [
'X-Sender-ID' => env('EMAIL_SENDER_ID'),
'Reply-To' => 'support@example.com',
],
Attachments
Use Laravel’s Swift_Attachment:
$emailService->send([
'to' => 'user@example.com',
'attachments' => [
(new \Swift_Attachment(
file_get_contents(storage_path('app/report.pdf')),
'report.pdf',
'application/pdf'
))->setDisposition('attachment'),
],
// ... rest of the config
]);
Events and Listeners
Listen for email events (e.g., EmailSent):
// In a service provider
$this->app->booted(function () {
\Austral\EmailBundle\Event\EmailSent::addListener(function ($event) {
// Log analytics or trigger side effects
});
});
Testing
Mock the EmailService in tests:
$emailService = $this->createMock(EmailService::class);
$emailService->expects($this->once())
->method('send')
->with([
'to' => 'user@example.com',
'subject' => 'Test',
]);
$this->app->instance(EmailService::class, $emailService);
Dependency Conflicts
austral/tools-bundle, austral/entity-bundle, and austral/entity-translate-bundle. Ensure these are installed and compatible:
composer require austral/tools-bundle austral/entity-bundle austral/entity-translate-bundle
composer.json for version constraints and run composer update.Template Paths
resources/views/emails/ or a subdirectory (e.g., emails/newsletters/welcome.blade.php).template key: 'template' => 'emails.newsletters.welcome'.Queue Configuration
emails queue by default. Ensure your queue worker is configured to process it:
php artisan queue:work --queue=emails
QUEUE_CONNECTION in .env if needed.Logging Overrides
email_log table. If you customize the EmailLog entity, run:
php artisan doctrine:migrations:diff
php artisan doctrine:migrations:migrate
Symfony Mailer vs. Swiftmailer
Mail facade, ensure consistency in configuration (e.g., transport settings).Check Logs
APP_DEBUG=true) to see email-sending errors in storage/logs/laravel.log.Swift_TransportException or Symfony\Component\Mailer\Exception\TransportException.Validate Email Data
dd() to inspect the email data before sending:
$emailData = [
'to' => 'user@example.com',
'subject' => 'Test',
'template' => 'emails.test',
'data' => ['key' => 'value'],
];
dd($emailData); // Verify structure
$emailService->send($emailData);
Test SMTP Locally
// config/austral_email.php
'transport' => 'smtp://mailhog:1025',
http://localhost:8025.Clear Cache
php artisan config:clear
php artisan cache:clear
Custom Email Log Fields
Extend the EmailLog entity to add fields like campaign_id or user_id:
// src/Entity/ExtendedEmailLog.php
namespace App\Entity;
use Austral\EmailBundle\Entity\EmailLog as BaseEmailLog;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class ExtendedEmailLog extends BaseEmailLog
{
#[ORM\Column]
private ?int $campaignId = null;
// Getters/setters...
}
Update the bundle’s EmailService to populate these fields.
Dynamic Templates Fetch templates dynamically (e.g., from a database):
$templateContent = $templateRepository->find($templateId)->getContent();
$emailService->send([
'to' => 'user@example.com',
'subject' => 'Dynamic Email',
'html' => $templateContent, // Skip view rendering
'data' => [],
]);
Email Validation Add validation rules for email data:
use Austral\Email
How can I help you explore Laravel packages today?