alexlbr/email
Provider-agnostic PHP email library with a simple adapter interface. Includes a SendGrid mailer implementation and a MailerInterface for adding your own providers by creating a Mailer adapter under the Mailer namespace. Install via Composer.
Installation:
composer require alexlbr/email
Add the service provider to config/app.php under providers:
Alexlbr\Email\EmailServiceProvider::class,
Publish Config:
php artisan vendor:publish --provider="Alexlbr\Email\EmailServiceProvider"
This generates a config file at config/email.php. Update SMTP/mailer settings here.
First Use Case: Send a basic email via a controller or command:
use Alexlbr\Email\Facades\Email;
Email::send('user@example.com', 'Welcome!', 'emails.welcome', ['name' => 'John']);
resources/views/emails/welcome.blade.php.Template-Based Emails:
resources/views/emails/.Email::send():
Email::send($to, $subject, 'emails.invoice', ['amount' => $total]);
Queueing Emails:
Email::queue('user@example.com', 'Reminder', 'emails.reminder', [], 'high');
.env (e.g., QUEUE_CONNECTION=database).Attachments:
Email::send($to, 'Subject', 'emails.report')
->attach('path/to/file.pdf')
->embed('path/to/image.png', 'image_id');
Custom Mailers:
namespace App\Mailers;
use Alexlbr\Email\Mailer;
class NewsletterMailer extends Mailer {
public function sendNewsletter($to, $data) {
return $this->send($to, 'Newsletter', 'emails.newsletter', $data);
}
}
Event::listen('user.registered', function ($user) {
Email::send($user->email, 'Welcome', 'emails.welcome', ['name' => $user->name]);
});
return response()->json(['status' => Email::send($to, $subject, $template, $data)->status()]);
$this->app->instance('email', Mockery::mock('overload:Alexlbr\Email\Facades\Email'));
Deprecated Package:
Mail facade or Illuminate\Mail.No Queue Middleware:
Mail facade, this package doesn’t natively support queue middleware (e.g., afterCommit). Workaround:
Email::queue(...)->onQueue('emails')->afterCommit();
Config Overrides:
mail.php. Merge settings manually:
'driver' => config('mail.driver'), // Sync with Laravel’s config
Template Caching:
php artisan view:clear
log method to the facade for debugging:
Email::send(...)->log(); // Check storage/logs/email.log
config/email.php:
'log_errors' => true,
'debug' => env('MAIL_DEBUG', false),
Custom Transport:
Override the transport layer by binding a new class to the email.transport service provider tag:
$this->app->bind('email.transport', function () {
return new CustomTransport();
});
Event Hooks:
Listen for email.sent and email.failed events (if the package emits them):
Event::listen('email.sent', function ($event) {
// Log or process sent emails
});
Fallback to Laravel Mail:
If the package fails, fall back to Laravel’s Mail facade:
try {
Email::send(...);
} catch (\Exception $e) {
\Mail::send(...); // Use Laravel’s Mail facade
}
How can I help you explore Laravel packages today?