Installation:
composer require callisto/callisto-mailer
php artisan vendor:publish --provider="Callisto\Mailer\CallistoMailerBundle" --tag="config"
Publish the migrations and templates:
php artisan vendor:publish --provider="Callisto\Mailer\CallistoMailerBundle" --tag="migrations"
php artisan migrate
First Use Case:
php artisan callisto:mailer:create-template (CLI) or manually via the templates table.use Callisto\Mailer\Mailer;
$mailer = app(Mailer::class);
$mailer->send([
'template_id' => 1, // ID of your template
'recipient' => '[email protected]',
'variables' => [
'name' => 'John Doe',
'unsubscribe_link' => url('/unsubscribe')
]
]);
Key Files to Review:
config/callisto_mailer.php (core configuration like default sender, layouts, and storage paths).database/migrations/ (template and layout table structures).resources/views/callisto_mailer/ (default Twig layouts; override these for custom designs).Template Management:
templates table with fields like name, subject, html_content, is_active, and layout_id.default.html.twig) or create custom ones in resources/views/callisto_mailer/layouts/.variables JSON field (e.g., {"name": "string", "is_active": "boolean"}). The mailer validates these before sending.Sending Emails:
$mailer->send([
'template_id' => $templateId,
'recipient' => $email,
'variables' => $data,
'subject_override' => 'Custom Subject' // Optional
]);
$mailer->queue([
'template_id' => 1,
'recipient' => '[email protected]',
'variables' => [...],
]);
Configure the queue in config/callisto_mailer.php under queue_connection.Localization:
translations table (linked via template_id and locale).locale in the send method:
$mailer->send([
'template_id' => 1,
'recipient' => '[email protected]',
'locale' => 'fr',
'variables' => [...]
]);
Event Listeners:
// In a service provider
$this->app->booted(function () {
CallistoMailerEvents::onTemplateSent(function ($event) {
// Log or process sent emails
});
});
Testing:
CallistoMailerTestCase trait for unit/feature tests:
use Callisto\Mailer\Testing\CallistoMailerTestCase;
class EmailTest extends CallistoMailerTestCase {
public function testSendEmail() {
$this->assertSentTo('[email protected]', 1); // template_id
}
}
Variable Validation Errors:
variables in the send method don’t match the template’s expected schema, the mailer throws a ValidationException.variables JSON field in the templates table and ensure the sent data matches.Layout Overrides:
resources/views/callisto_mailer/layouts/ must extend the base layout (default.html.twig) or implement the same Twig blocks (body, header, etc.).{{ block('body') }} in custom layouts to ensure compatibility.Queue Configuration:
callisto_mailer.queue_connection is set in the config (e.g., sync, database, or redis).php artisan queue:work in development to process queued emails.Twig Security:
safe filter in Twig:
{{ variables.some_html|safe }}
Database Locking:
DB::transaction(function () {
$template = Template::find($id);
$template->update(['html_content' => $newHtml]);
});
Log Sent Emails:
Enable logging in config/callisto_mailer.php:
'logging' => [
'enabled' => true,
'channel' => 'mailer',
],
Check logs in storage/logs/mailer.log.
Template Preview:
Use the callisto:mailer:preview CLI command to render templates locally:
php artisan callisto:mailer:preview 1 --variables='{"name":"Test"}'
Common Errors:
template_id exists in the templates table.layout_id in the template matches an entry in the layouts table or a file in resources/views/callisto_mailer/layouts/.Custom Storage:
Override the default template storage (e.g., use S3) by implementing Callisto\Mailer\Storage\TemplateStorageInterface and binding it in the service container.
Dynamic Layouts:
Fetch layouts dynamically from an API or database by extending the LayoutResolver service.
Event Extensions:
Dispatch custom events by extending the Mailer class and overriding the send method:
class CustomMailer extends Mailer {
protected function afterSend($event) {
event(new CustomEmailSent($event->getData()));
}
}
Testing Helpers:
Extend CallistoMailerTestCase to add custom assertions or mocks for your use case.
```markdown
### Configuration Quirks
1. **Default Sender**:
The `from_email` and `from_name` in `config/callisto_mailer.php` are used if not overridden per email. Set them explicitly in the send method if needed:
```php
$mailer->send([
'template_id' => 1,
'from_email' => '[email protected]',
'from_name' => 'Your App',
// ...
]);
Attachment Handling:
The package doesn’t natively support attachments. Use Laravel’s SwiftMailer integration to attach files:
$mailer->send([
'template_id' => 1,
'attachments' => [
'path/to/file.pdf' => 'filename.pdf',
],
// ...
]);
Ensure config/callisto_mailer.php has 'attachments_enabled' => true.
Rate Limiting: Implement rate limiting via middleware or the queue system to avoid hitting email provider thresholds (e.g., Gmail’s 500 emails/day limit).
How can I help you explore Laravel packages today?