Installation
composer require cleentfaar/mailer-bundle
Add to config/bundles.php (Symfony) or config/app.php (Laravel via Symfony bridge):
Cleentfaar\MailerBundle\CleentfaarMailerBundle::class => ['all' => true],
Configuration Publish the default config:
php artisan vendor:publish --provider="Cleentfaar\MailerBundle\CleentfaarMailerBundle" --tag="config"
Edit config/cleentfaar_mailer.php to define your mailers (e.g., smtp, sendgrid).
First Use Case Inject the mailer service into a controller/service:
use Cleentfaar\MailerBundle\Mailer\MailerInterface;
class UserController extends Controller
{
public function __construct(private MailerInterface $mailer) {}
public function sendWelcomeEmail()
{
$this->mailer->send(
new Email('welcome@example.com', 'Welcome!')
->from('noreply@example.com')
->text('Hello, welcome!')
);
}
}
Email Composition
Use the Email class to build emails programmatically:
$email = new Email('user@example.com', 'Subject')
->from('app@example.com')
->html('<h1>Hello</h1>')
->attach('path/to/file.pdf');
Transport Integration
Configure transports in config/cleentfaar_mailer.php:
'transports' => [
'smtp' => [
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
// ...
],
'sendgrid' => [
'api_key' => env('SENDGRID_API_KEY'),
],
],
Dynamic Mailer Selection
Use the MailerInterface to switch transports at runtime:
$this->mailer->setTransport('sendgrid')->send($email);
Event Listeners Attach listeners for pre/post-send hooks:
$mailer->addListener(new class implements MailerListenerInterface {
public function onSend(Email $email, TransportInterface $transport) {
// Log or modify email before sending
}
});
Testing
Use the NullTransport for unit tests:
$mailer = new Mailer(new NullTransport());
$mailer->send($email); // No actual sending
Transport Configuration
default_transport in config will throw RuntimeException..env to avoid config parsing errors.Email Validation
egulias/email-validator if needed.Attachments
max_file_size is configured.Symfony vs. Laravel
Symfony\Component\HttpKernel\Kernel directly.Enable debug mode in config:
'debug' => env('APP_DEBUG', false),
Logs sent emails to storage/logs/mailer.log.
Check transport-specific errors in var/log/dev.log (Symfony) or Laravel’s storage/logs/laravel.log.
Custom Transports
Implement Cleentfaar\MailerBundle\Transport\TransportInterface:
class MyTransport implements TransportInterface {
public function send(Email $email): void {
// Custom logic (e.g., HTTP API calls)
}
}
Register in config:
'transports' => [
'my_transport' => MyTransport::class,
],
Email Events
Extend Cleentfaar\MailerBundle\Event\EmailEvent for custom events (e.g., EmailSent).
Templating
Integrate with Twig or Blade by extending the Email class:
$email->html(view('emails.welcome', ['user' => $user])->render());
Mailer::flush() to send queued emails in bulk.How can I help you explore Laravel packages today?