Installation:
composer require devster/rad-mailer
Add to config/app.php under providers:
Devster\RadMailer\RadMailerServiceProvider::class,
Publish config (optional):
php artisan vendor:publish --provider="Devster\RadMailer\RadMailerServiceProvider"
First Use Case: Send a basic email in a controller:
use Rad\Mailer;
public function sendWelcomeEmail()
{
$mailer = app(Mailer::class);
$mailer->send([
'to' => 'user@example.com',
'subject' => 'Welcome!',
'template' => 'emails.welcome', // Twig template path
'data' => ['name' => 'John']
]);
}
Key Files:
config/rad-mailer.php (for default settings)resources/views/emails/ (store Twig templates here)Twig Integration:
emails.welcome.twig).{# emails/welcome.twig #}
<p>Hello {{ data.name }}, welcome!</p>
$mailer->send(['template' => 'emails.welcome', 'data' => ['name' => 'Alice']]);
Swiftmailer Configuration:
config/mail.php (RAD Mailer uses Swiftmailer under the hood).config/rad-mailer.php:
'default_from' => 'noreply@example.com',
'twig_path' => resource_path('views'),
Batch Sending:
$mailer->send([
'to' => ['user1@example.com', 'user2@example.com'],
'subject' => 'Monthly Newsletter',
'template' => 'emails.newsletter',
'data' => ['month' => 'June']
]);
Attachments:
$mailer->send([
'to' => 'user@example.com',
'subject' => 'Your Invoice',
'template' => 'emails.invoice',
'attachments' => [storage_path('app/invoice.pdf')]
]);
Queueing Emails:
$mailer->queue([
'to' => 'user@example.com',
'subject' => 'Delayed Email',
'template' => 'emails.delayed',
'data' => ['content' => 'Hello!']
]);
Customizing the Mailer Instance:
AppServiceProvider:
$this->app->bind(Mailer::class, function ($app) {
$swift = $app->make(Swift_Mailer::class);
$twig = $app->make('view');
return new Rad\Mailer($swift, $twig, 'custom@example.com');
});
Twig Template Paths:
resources/views/ or update twig_path in config.emails.welcome resolves to resources/views/emails/welcome.twig).Swiftmailer Configuration:
config/mail.php (SMTP/Gmail settings, etc.).Data Binding:
data to be an array. If passing an object, ensure properties are accessible (e.g., $data->name in Twig).data to prevent Twig errors.Queueing Issues:
php artisan queue:work --verbose).queue method is used instead of send for async emails.Default from Address:
from address per email or set it in the constructor. Unset defaults may cause emails to fail.Enable Swiftmailer Logging:
Add to config/mail.php:
'debug' => env('MAIL_DEBUG', false),
Check logs in storage/logs/laravel.log.
Validate Templates: Test Twig templates independently:
$twig = $app->make('view');
echo $twig->render('emails.welcome', ['name' => 'Test']);
Check for Exceptions: Wrap RAD Mailer calls in try-catch:
try {
$mailer->send([...]);
} catch (\Exception $e) {
\Log::error('Email failed: ' . $e->getMessage());
}
Custom Twig Environment:
Extend the Twig environment in AppServiceProvider:
$twig = $app->make('view');
$twig->getEnvironment()->addGlobal('app_name', config('app.name'));
Pre/Post-Send Hooks:
Override the Rad\Mailer class to add logic:
class CustomMailer extends Rad\Mailer {
public function send($params) {
// Pre-send logic
$result = parent::send($params);
// Post-send logic
return $result;
}
}
Dynamic Templates:
Use Twig’s embed or include for modular templates:
{# emails/base.twig #}
<html>
<body>
{{ include('emails/header') }}
{{ block('content') }}
</body>
</html>
Testing: Mock the mailer in tests:
$mailer = Mockery::mock(Rad\Mailer::class);
$mailer->shouldReceive('send')->once();
$this->app->instance(Rad\Mailer::class, $mailer);
How can I help you explore Laravel packages today?