cleentfaar/mailer
Laravel-compatible mailer package providing a simple API to send emails through configurable transports. Includes basic setup for drivers, message composition, and delivery handling to integrate email sending into your application workflows.
Installation
composer require cleentfaar/mailer
Publish the config file (if needed):
php artisan vendor:publish --provider="Cleentfaar\Mailer\MailerServiceProvider" --tag="mailer-config"
First Use Case: Sending a Basic Email
use Cleentfaar\Mailer\Facades\Mailer;
Mailer::send('emails.welcome', [], function ($message) {
$message->to('[email protected]')
->subject('Welcome!');
});
Where to Look First
config/mailer.php (if published) for customization.resources/views/emails/.Dynamic Email Templates Use Blade templates with dynamic data:
Mailer::send('emails.invoice', ['amount' => 99.99, 'user' => $user], function ($message) {
$message->to($user->email)
->subject('Invoice #'.$invoiceId);
});
Reusable Email Classes
Extend Cleentfaar\Mailer\Mailer for reusable logic:
class UserMailer extends Mailer {
public function sendWelcome(User $user) {
$this->send('emails.welcome', ['user' => $user], function ($message) use ($user) {
$message->to($user->email)
->subject('Welcome, '.$user->name);
});
}
}
Attachments & Inline Images
$message->attach('path/to/file.pdf')
->attachData($pdfContent, 'invoice.pdf', ['mime' => 'application/pdf'])
->embed('path/to/image.png', 'image_id');
Queueing Emails Use Laravel's queue system:
Mailer::queue('emails.notification', [], function ($message) {
$message->to('[email protected]');
});
Customizing the Mailer Instance
Bind a custom mailer in AppServiceProvider:
$this->app->bind('mailer', function () {
return new CustomMailer();
});
Leverage Laravel's Existing Mail Config
The package integrates seamlessly with Laravel's config/mail.php (e.g., SMTP, Mailgun, SES).
Use Events for Post-Send Logic
Listen to Cleentfaar\Mailer\Events\MessageSent:
Event::listen('Cleentfaar\Mailer\Events\MessageSent', function ($event) {
Log::info('Email sent to: '.$event->message->to->address);
});
Testing Emails
Use Laravel's Mail::fake() alongside the package:
Mail::fake();
Mailer::send('emails.test');
Mail::assertSent(\Cleentfaar\Mailer\Message::class);
Localization Support Pass a locale to templates:
Mailer::send('emails.welcome', [], function ($message) {
$message->locale('nl');
});
Template Paths
Ensure Blade templates are in resources/views/emails/ or specify a custom path in the config:
'view_path' => resource_path('views/custom_emails'),
Queueing Without a Queue Driver
If MAIL_DRIVER is not set to queue, queued emails will send immediately (bypassing the queue).
Missing Facade Alias
If using the facade, ensure the alias is registered in config/app.php:
'aliases' => [
'Mailer' => Cleentfaar\Mailer\Facades\Mailer::class,
],
Attachment Size Limits
Large attachments may hit PHP's post_max_size or upload_max_filesize. Adjust these in php.ini or use cloud storage (e.g., S3).
Caching Templates The package does not cache templates by default. For high-volume emails, consider caching compiled views:
Mailer::setViewFactory(function () {
return new \Illuminate\View\Factory(app('view'), app('events'));
});
Log Sent Emails
Enable logging in config/mailer.php:
'log_sent_messages' => true,
Check storage/logs/laravel.log for sent email details.
Inspect Raw Messages Use a debug listener:
Event::listen('Cleentfaar\Mailer\Events\MessageSent', function ($event) {
\Log::debug('Raw Message:', $event->message->getSwiftMessage()->toString());
});
Check for Silent Failures Wrap sends in try-catch:
try {
Mailer::send('emails.error');
} catch (\Exception $e) {
\Log::error('Email failed: '.$e->getMessage());
}
Custom Mailer Drivers
Extend Cleentfaar\Mailer\Mailer and bind it in AppServiceProvider:
class SlackMailer extends Mailer {
public function send($view, $data, Closure $callback) {
// Custom logic (e.g., send to Slack)
}
}
Modify Message Before Sending
Use the beforeSend event:
Event::listen('Cleentfaar\Mailer\Events\BeforeSend', function ($event) {
$event->message->cc('[email protected]');
});
Add Custom Headers
$message->addHeader('X-Priority', 'High');
$message->addHeader('Reply-To', '[email protected]');
Override Default Swiftmailer Transport
Configure in config/mail.php:
'transport' => 'smtp',
'smtp' => [
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
// Custom transport options
],
Dynamic Recipients
Use closures for dynamic to, cc, or bcc:
$message->to(function ($message) {
return $message->to('[email protected]');
});
How can I help you explore Laravel packages today?